Show More
@@ -1,581 +1,592 b'' | |||||
1 | """ History related magics and functionality """ |
|
1 | """ History related magics and functionality """ | |
2 | #----------------------------------------------------------------------------- |
|
2 | #----------------------------------------------------------------------------- | |
3 | # Copyright (C) 2010 The IPython Development Team. |
|
3 | # Copyright (C) 2010 The IPython Development Team. | |
4 | # |
|
4 | # | |
5 | # Distributed under the terms of the BSD License. |
|
5 | # Distributed under the terms of the BSD License. | |
6 | # |
|
6 | # | |
7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | from __future__ import print_function |
|
13 | from __future__ import print_function | |
14 |
|
14 | |||
15 | # Stdlib imports |
|
15 | # Stdlib imports | |
16 | import os |
|
16 | import os | |
17 | import re |
|
17 | import re | |
18 | import sqlite3 |
|
18 | import sqlite3 | |
19 |
|
19 | |||
20 | # Our own packages |
|
20 | # Our own packages | |
21 | from IPython.config.configurable import Configurable |
|
21 | from IPython.config.configurable import Configurable | |
22 | import IPython.utils.io |
|
22 | import IPython.utils.io | |
23 |
|
23 | |||
24 | from IPython.testing import decorators as testdec |
|
24 | from IPython.testing import decorators as testdec | |
25 | from IPython.utils.io import ask_yes_no |
|
25 | from IPython.utils.io import ask_yes_no | |
26 | from IPython.utils.traitlets import Bool, Dict, Instance, Int, List, Unicode |
|
26 | from IPython.utils.traitlets import Bool, Dict, Instance, Int, List, Unicode | |
27 | from IPython.utils.warn import warn |
|
27 | from IPython.utils.warn import warn | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Classes and functions |
|
30 | # Classes and functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | class HistoryManager(Configurable): |
|
33 | class HistoryManager(Configurable): | |
34 | """A class to organize all history-related functionality in one place. |
|
34 | """A class to organize all history-related functionality in one place. | |
35 | """ |
|
35 | """ | |
36 | # Public interface |
|
36 | # Public interface | |
37 |
|
37 | |||
38 | # An instance of the IPython shell we are attached to |
|
38 | # An instance of the IPython shell we are attached to | |
39 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
39 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
40 | # Lists to hold processed and raw history. These start with a blank entry |
|
40 | # Lists to hold processed and raw history. These start with a blank entry | |
41 | # so that we can index them starting from 1 |
|
41 | # so that we can index them starting from 1 | |
42 | input_hist_parsed = List([""]) |
|
42 | input_hist_parsed = List([""]) | |
43 | input_hist_raw = List([""]) |
|
43 | input_hist_raw = List([""]) | |
44 | # A list of directories visited during session |
|
44 | # A list of directories visited during session | |
45 | dir_hist = List() |
|
45 | dir_hist = List() | |
46 | # A dict of output history, keyed with ints from the shell's execution count |
|
46 | # A dict of output history, keyed with ints from the shell's execution count | |
47 | output_hist = Dict() |
|
47 | output_hist = Dict() | |
48 | # String holding the path to the history file |
|
48 | # String holding the path to the history file | |
49 | hist_file = Unicode() |
|
49 | hist_file = Unicode() | |
50 | # The SQLite database |
|
50 | # The SQLite database | |
51 | db = Instance(sqlite3.Connection) |
|
51 | db = Instance(sqlite3.Connection) | |
52 | # The number of the current session in the history database |
|
52 | # The number of the current session in the history database | |
53 | session_number = Int() |
|
53 | session_number = Int() | |
54 | # Should we log output to the database? (default no) |
|
54 | # Should we log output to the database? (default no) | |
55 | db_log_output = Bool(False, config=True) |
|
55 | db_log_output = Bool(False, config=True) | |
56 | # Write to database every x commands (higher values save disk access & power) |
|
56 | # Write to database every x commands (higher values save disk access & power) | |
57 | # Values of 1 or less effectively disable caching. |
|
57 | # Values of 1 or less effectively disable caching. | |
58 | db_cache_size = Int(0, config=True) |
|
58 | db_cache_size = Int(0, config=True) | |
59 | # The input and output caches |
|
59 | # The input and output caches | |
60 | db_input_cache = List() |
|
60 | db_input_cache = List() | |
61 | db_output_cache = List() |
|
61 | db_output_cache = List() | |
62 |
|
62 | |||
63 | # Private interface |
|
63 | # Private interface | |
64 | # Variables used to store the three last inputs from the user. On each new |
|
64 | # Variables used to store the three last inputs from the user. On each new | |
65 | # history update, we populate the user's namespace with these, shifted as |
|
65 | # history update, we populate the user's namespace with these, shifted as | |
66 | # necessary. |
|
66 | # necessary. | |
67 | _i00, _i, _ii, _iii = '','','','' |
|
67 | _i00, _i, _ii, _iii = '','','','' | |
68 |
|
68 | |||
69 | # A set with all forms of the exit command, so that we don't store them in |
|
69 | # A set with all forms of the exit command, so that we don't store them in | |
70 | # the history (it's annoying to rewind the first entry and land on an exit |
|
70 | # the history (it's annoying to rewind the first entry and land on an exit | |
71 | # call). |
|
71 | # call). | |
72 | _exit_commands = None |
|
72 | _exit_commands = None | |
73 |
|
73 | |||
74 | def __init__(self, shell, config=None): |
|
74 | def __init__(self, shell, config=None): | |
75 | """Create a new history manager associated with a shell instance. |
|
75 | """Create a new history manager associated with a shell instance. | |
76 | """ |
|
76 | """ | |
77 | # We need a pointer back to the shell for various tasks. |
|
77 | # We need a pointer back to the shell for various tasks. | |
78 | super(HistoryManager, self).__init__(shell=shell, config=config) |
|
78 | super(HistoryManager, self).__init__(shell=shell, config=config) | |
79 |
|
79 | |||
80 | # list of visited directories |
|
80 | # list of visited directories | |
81 | try: |
|
81 | try: | |
82 | self.dir_hist = [os.getcwd()] |
|
82 | self.dir_hist = [os.getcwd()] | |
83 | except OSError: |
|
83 | except OSError: | |
84 | self.dir_hist = [] |
|
84 | self.dir_hist = [] | |
85 |
|
85 | |||
86 | # Now the history file |
|
86 | # Now the history file | |
87 | if shell.profile: |
|
87 | if shell.profile: | |
88 | histfname = 'history-%s' % shell.profile |
|
88 | histfname = 'history-%s' % shell.profile | |
89 | else: |
|
89 | else: | |
90 | histfname = 'history' |
|
90 | histfname = 'history' | |
91 | self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite') |
|
91 | self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite') | |
92 | self.init_db() |
|
92 | self.init_db() | |
93 |
|
93 | |||
94 | self._i00, self._i, self._ii, self._iii = '','','','' |
|
94 | self._i00, self._i, self._ii, self._iii = '','','','' | |
95 |
|
95 | |||
96 | self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit', |
|
96 | self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit', | |
97 | '%quit', '%Exit', '%exit']) |
|
97 | '%quit', '%Exit', '%exit']) | |
98 |
|
98 | |||
99 | def init_db(self): |
|
99 | def init_db(self): | |
100 | """Connect to the database and get new session number.""" |
|
100 | """Connect to the database and get new session number.""" | |
101 | self.db = sqlite3.connect(self.hist_file) |
|
101 | self.db = sqlite3.connect(self.hist_file) | |
102 | self.db.execute("""CREATE TABLE IF NOT EXISTS history |
|
102 | self.db.execute("""CREATE TABLE IF NOT EXISTS history | |
103 | (session integer, line integer, source text, source_raw text, |
|
103 | (session integer, line integer, source text, source_raw text, | |
104 | PRIMARY KEY (session, line))""") |
|
104 | PRIMARY KEY (session, line))""") | |
105 | # Output history is optional, but ensure the table's there so it can be |
|
105 | # Output history is optional, but ensure the table's there so it can be | |
106 | # enabled later. |
|
106 | # enabled later. | |
107 | self.db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
107 | self.db.execute("""CREATE TABLE IF NOT EXISTS output_history | |
108 | (session integer, line integer, output text, |
|
108 | (session integer, line integer, output text, | |
109 | PRIMARY KEY (session, line))""") |
|
109 | PRIMARY KEY (session, line))""") | |
110 | cur = self.db.execute("""SELECT name FROM sqlite_master WHERE |
|
110 | cur = self.db.execute("""SELECT name FROM sqlite_master WHERE | |
111 | type='table' AND name='singletons'""") |
|
111 | type='table' AND name='singletons'""") | |
112 | if not cur.fetchone(): |
|
112 | if not cur.fetchone(): | |
113 | self.db.execute("""CREATE TABLE singletons |
|
113 | self.db.execute("""CREATE TABLE singletons | |
114 | (name text PRIMARY KEY, value)""") |
|
114 | (name text PRIMARY KEY, value)""") | |
115 | self.db.execute("""INSERT INTO singletons VALUES |
|
115 | self.db.execute("""INSERT INTO singletons VALUES | |
116 | ('session_number', 1)""") |
|
116 | ('session_number', 1)""") | |
117 | self.db.commit() |
|
117 | self.db.commit() | |
118 | cur = self.db.execute("""SELECT value FROM singletons WHERE |
|
118 | cur = self.db.execute("""SELECT value FROM singletons WHERE | |
119 | name='session_number'""") |
|
119 | name='session_number'""") | |
120 | self.session_number = cur.fetchone()[0] |
|
120 | self.session_number = cur.fetchone()[0] | |
121 |
|
121 | |||
122 | #Increment by one for next session. |
|
122 | #Increment by one for next session. | |
123 | self.db.execute("""UPDATE singletons SET value=? WHERE |
|
123 | self.db.execute("""UPDATE singletons SET value=? WHERE | |
124 | name='session_number'""", (self.session_number+1,)) |
|
124 | name='session_number'""", (self.session_number+1,)) | |
125 | self.db.commit() |
|
125 | self.db.commit() | |
126 |
|
126 | |||
127 | def get_hist_tail(self, n=10, raw=True): |
|
127 | def get_hist_tail(self, n=10, raw=True, output=False): | |
128 | """Get the last n lines from the history database.""" |
|
128 | """Get the last n lines from the history database.""" | |
129 | toget = 'source_raw' if raw else 'source' |
|
129 | toget = 'source_raw' if raw else 'source' | |
|
130 | sqlfrom = "history" | |||
|
131 | if output: | |||
|
132 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" | |||
|
133 | toget = "history.%s, output_history.output" % toget | |||
130 | cur = self.db.execute("SELECT session, line, " + toget +\ |
|
134 | cur = self.db.execute("SELECT session, line, " + toget +\ | |
131 |
" FROM |
|
135 | " FROM "+sqlfrom+" ORDER BY session DESC, line DESC LIMIT ?", (n,)) | |
132 |
|
|
136 | hist = reversed(cur.fetchall()) | |
|
137 | if output: | |||
|
138 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist) | |||
|
139 | return hist | |||
133 |
|
140 | |||
134 | def get_hist_search(self, pattern="*", raw=True): |
|
141 | def get_hist_search(self, pattern="*", raw=True): | |
135 | """Search the database using unix glob-style matching (wildcards * and |
|
142 | """Search the database using unix glob-style matching (wildcards * and | |
136 | ?, escape using \). |
|
143 | ?, escape using \). | |
137 |
|
144 | |||
138 | Returns |
|
145 | Returns | |
139 | ------- |
|
146 | ------- | |
140 | An iterator over tuples: (session, line_number, command) |
|
147 | An iterator over tuples: (session, line_number, command) | |
141 | """ |
|
148 | """ | |
142 | toget = "source_raw" if raw else source |
|
149 | toget = "source_raw" if raw else source | |
143 | return self.db.execute("SELECT session, line, " +toget+ \ |
|
150 | return self.db.execute("SELECT session, line, " +toget+ \ | |
144 | "FROM history WHERE" +toget+ "GLOB ?", (pattern,)) |
|
151 | "FROM history WHERE" +toget+ "GLOB ?", (pattern,)) | |
145 |
|
152 | |||
146 | def _get_hist_session(self, start=1, stop=None, raw=True, output=False): |
|
153 | def _get_hist_session(self, start=1, stop=None, raw=True, output=False): | |
147 | """Get input and output history from the current session. Called by |
|
154 | """Get input and output history from the current session. Called by | |
148 | get_history, and takes similar parameters.""" |
|
155 | get_history, and takes similar parameters.""" | |
149 | input_hist = self.input_hist_raw if raw else self.input_hist_parsed |
|
156 | input_hist = self.input_hist_raw if raw else self.input_hist_parsed | |
150 |
|
157 | |||
151 | n = len(input_hist) |
|
158 | n = len(input_hist) | |
152 | if start < 0: |
|
159 | if start < 0: | |
153 | start += n |
|
160 | start += n | |
154 | if not stop: |
|
161 | if not stop: | |
155 | stop = n |
|
162 | stop = n | |
156 | elif stop < 0: |
|
163 | elif stop < 0: | |
157 | stop += n |
|
164 | stop += n | |
158 |
|
165 | |||
159 | for i in range(start, stop): |
|
166 | for i in range(start, stop): | |
160 | if output: |
|
167 | if output: | |
161 | line = (input_hist[i], self.output_hist.get(i)) |
|
168 | line = (input_hist[i], repr(self.output_hist.get(i))) | |
162 | else: |
|
169 | else: | |
163 | line = input_hist[i] |
|
170 | line = input_hist[i] | |
164 | yield (0, i, line) |
|
171 | yield (0, i, line) | |
165 |
|
172 | |||
166 | def get_history(self, session=0, start=1, stop=None, raw=True,output=False): |
|
173 | def get_history(self, session=0, start=1, stop=None, raw=True,output=False): | |
167 | """Retrieve input by session. |
|
174 | """Retrieve input by session. | |
168 |
|
175 | |||
169 | Parameters |
|
176 | Parameters | |
170 | ---------- |
|
177 | ---------- | |
171 | session : int |
|
178 | session : int | |
172 | Session number to retrieve. The current session is 0, and negative |
|
179 | Session number to retrieve. The current session is 0, and negative | |
173 | numbers count back from current session, so -1 is previous session. |
|
180 | numbers count back from current session, so -1 is previous session. | |
174 | start : int |
|
181 | start : int | |
175 | First line to retrieve. |
|
182 | First line to retrieve. | |
176 | stop : int |
|
183 | stop : int | |
177 | End of line range (excluded from output itself). If None, retrieve |
|
184 | End of line range (excluded from output itself). If None, retrieve | |
178 | to the end of the session. |
|
185 | to the end of the session. | |
179 | raw : bool |
|
186 | raw : bool | |
180 | If True, return untranslated input |
|
187 | If True, return untranslated input | |
181 | output : bool |
|
188 | output : bool | |
182 | If True, attempt to include output. This will be 'real' Python |
|
189 | If True, attempt to include output. This will be 'real' Python | |
183 | objects for the current session, or text reprs from previous |
|
190 | objects for the current session, or text reprs from previous | |
184 | sessions if db_log_output was enabled at the time. Where no output |
|
191 | sessions if db_log_output was enabled at the time. Where no output | |
185 | is found, None is used. |
|
192 | is found, None is used. | |
186 |
|
193 | |||
187 | Returns |
|
194 | Returns | |
188 | ------- |
|
195 | ------- | |
189 | An iterator over the desired lines. Each line is a 3-tuple, either |
|
196 | An iterator over the desired lines. Each line is a 3-tuple, either | |
190 | (session, line, input) if output is False, or |
|
197 | (session, line, input) if output is False, or | |
191 | (session, line, (input, output)) if output is True. |
|
198 | (session, line, (input, output)) if output is True. | |
192 | """ |
|
199 | """ | |
193 | if session == 0 or session==self.session_number: # Current session |
|
200 | if session == 0 or session==self.session_number: # Current session | |
194 | return self._get_hist_session(start, stop, raw, output) |
|
201 | return self._get_hist_session(start, stop, raw, output) | |
195 | if session < 0: |
|
202 | if session < 0: | |
196 | session += self.session_number |
|
203 | session += self.session_number | |
197 |
|
204 | |||
198 | # Assemble the SQL query: |
|
205 | # Assemble the SQL query: | |
199 | sqlfrom = "history" |
|
206 | sqlfrom = "history" | |
200 |
toget = |
|
207 | toget = 'source_raw' if raw else 'source' | |
201 | if output: |
|
208 | if output: | |
202 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" |
|
209 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" | |
203 | toget = "history.%s, output_history.output" % toget |
|
210 | toget = "history.%s, output_history.output" % toget | |
204 | if stop: |
|
211 | if stop: | |
205 | lineclause = "line >= ? AND line < ?" |
|
212 | lineclause = "line >= ? AND line < ?" | |
206 | params = (session, start, stop) |
|
213 | params = (session, start, stop) | |
207 | else: |
|
214 | else: | |
208 | lineclause = "line>=?" |
|
215 | lineclause = "line>=?" | |
209 | params = (session, start) |
|
216 | params = (session, start) | |
210 |
|
217 | |||
211 |
cur = self.db.execute("SELECT %s FROM %s WHERE |
|
218 | cur = self.db.execute("""SELECT session, line, %s FROM %s WHERE | |
212 |
|
|
219 | session==? AND %s""" %(toget, sqlfrom, lineclause), params) | |
213 | if output: # Regroup into 3-tuples |
|
220 | if output: # Regroup into 3-tuples | |
214 | return ((ses, lin (inp, out)) for ses, lin, inp, out in cur) |
|
221 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur) | |
215 | return cur |
|
222 | return cur | |
216 |
|
223 | |||
217 | def get_hist_from_rangestr(self, rangestr, raw=True, output=False): |
|
224 | def get_hist_from_rangestr(self, rangestr, raw=True, output=False): | |
218 | """Get lines of history from a string of ranges, as used by magic |
|
225 | """Get lines of history from a string of ranges, as used by magic | |
219 | commands %hist, %save, %macro, etc.""" |
|
226 | commands %hist, %save, %macro, etc.""" | |
220 | for sess, s, e in extract_hist_ranges(rangestr): |
|
227 | for sess, s, e in extract_hist_ranges(rangestr): | |
221 | for line in self.get_history(sess, s, e, raw=raw, output=output): |
|
228 | for line in self.get_history(sess, s, e, raw=raw, output=output): | |
222 | yield line |
|
229 | yield line | |
223 |
|
230 | |||
224 | def store_inputs(self, line_num, source, source_raw=None): |
|
231 | def store_inputs(self, line_num, source, source_raw=None): | |
225 | """Store source and raw input in history and create input cache |
|
232 | """Store source and raw input in history and create input cache | |
226 | variables _i*. |
|
233 | variables _i*. | |
227 |
|
234 | |||
228 | Parameters |
|
235 | Parameters | |
229 | ---------- |
|
236 | ---------- | |
230 | line_num : int |
|
237 | line_num : int | |
231 | The prompt number of this input. |
|
238 | The prompt number of this input. | |
232 |
|
239 | |||
233 | source : str |
|
240 | source : str | |
234 | Python input. |
|
241 | Python input. | |
235 |
|
242 | |||
236 | source_raw : str, optional |
|
243 | source_raw : str, optional | |
237 | If given, this is the raw input without any IPython transformations |
|
244 | If given, this is the raw input without any IPython transformations | |
238 | applied to it. If not given, ``source`` is used. |
|
245 | applied to it. If not given, ``source`` is used. | |
239 | """ |
|
246 | """ | |
240 | if source_raw is None: |
|
247 | if source_raw is None: | |
241 | source_raw = source |
|
248 | source_raw = source | |
242 |
|
249 | |||
243 | # do not store exit/quit commands |
|
250 | # do not store exit/quit commands | |
244 | if source_raw.strip() in self._exit_commands: |
|
251 | if source_raw.strip() in self._exit_commands: | |
245 | return |
|
252 | return | |
246 |
|
253 | |||
247 | self.input_hist_parsed.append(source.rstrip()) |
|
254 | self.input_hist_parsed.append(source.rstrip()) | |
248 | self.input_hist_raw.append(source_raw.rstrip()) |
|
255 | self.input_hist_raw.append(source_raw.rstrip()) | |
249 |
|
256 | |||
250 | self.db_input_cache.append((self.session_number, line_num, |
|
257 | self.db_input_cache.append((self.session_number, line_num, | |
251 | source, source_raw)) |
|
258 | source, source_raw)) | |
252 | # Trigger to flush cache and write to DB. |
|
259 | # Trigger to flush cache and write to DB. | |
253 | if len(self.db_input_cache) >= self.db_cache_size: |
|
260 | if len(self.db_input_cache) >= self.db_cache_size: | |
254 | self.writeout_cache() |
|
261 | self.writeout_cache() | |
255 |
|
262 | |||
256 | # update the auto _i variables |
|
263 | # update the auto _i variables | |
257 | self._iii = self._ii |
|
264 | self._iii = self._ii | |
258 | self._ii = self._i |
|
265 | self._ii = self._i | |
259 | self._i = self._i00 |
|
266 | self._i = self._i00 | |
260 | self._i00 = source_raw |
|
267 | self._i00 = source_raw | |
261 |
|
268 | |||
262 | # hackish access to user namespace to create _i1,_i2... dynamically |
|
269 | # hackish access to user namespace to create _i1,_i2... dynamically | |
263 | new_i = '_i%s' % line_num |
|
270 | new_i = '_i%s' % line_num | |
264 | to_main = {'_i': self._i, |
|
271 | to_main = {'_i': self._i, | |
265 | '_ii': self._ii, |
|
272 | '_ii': self._ii, | |
266 | '_iii': self._iii, |
|
273 | '_iii': self._iii, | |
267 | new_i : self._i00 } |
|
274 | new_i : self._i00 } | |
268 | self.shell.user_ns.update(to_main) |
|
275 | self.shell.user_ns.update(to_main) | |
269 |
|
276 | |||
270 | def store_output(self, line_num, output): |
|
277 | def store_output(self, line_num, output): | |
271 | if not self.db_log_output: |
|
278 | if not self.db_log_output: | |
272 | return |
|
279 | return | |
273 | db_row = (self.session_number, line_num, output) |
|
280 | db_row = (self.session_number, line_num, output) | |
274 | if self.db_cache_size > 1: |
|
281 | if self.db_cache_size > 1: | |
275 | self.db_output_cache.append(db_row) |
|
282 | self.db_output_cache.append(db_row) | |
276 | else: |
|
283 | else: | |
277 | with self.db: |
|
284 | with self.db: | |
278 | self.db.execute("INSERT INTO output_history VALUES (?,?,?)", db_row) |
|
285 | self.db.execute("INSERT INTO output_history VALUES (?,?,?)", db_row) | |
279 |
|
286 | |||
280 | def writeout_cache(self): |
|
287 | def writeout_cache(self): | |
|
288 | #print(self.db_input_cache) | |||
281 | with self.db: |
|
289 | with self.db: | |
282 | self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)", |
|
290 | self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)", | |
283 | self.db_input_cache) |
|
291 | self.db_input_cache) | |
284 | self.db.executemany("INSERT INTO output_history VALUES (?, ?, ?)", |
|
292 | self.db.executemany("INSERT INTO output_history VALUES (?, ?, ?)", | |
285 | self.db_output_cache) |
|
293 | self.db_output_cache) | |
286 | self.db_input_cache = [] |
|
294 | self.db_input_cache = [] | |
287 | self.db_output_cache = [] |
|
295 | self.db_output_cache = [] | |
288 |
|
296 | |||
289 | def sync_inputs(self): |
|
297 | def sync_inputs(self): | |
290 | """Ensure raw and translated histories have same length.""" |
|
298 | """Ensure raw and translated histories have same length.""" | |
291 | lr = len(self.input_hist_raw) |
|
299 | lr = len(self.input_hist_raw) | |
292 | lp = len(self.input_hist_parsed) |
|
300 | lp = len(self.input_hist_parsed) | |
293 | if lp < lr: |
|
301 | if lp < lr: | |
294 | self.input_hist_raw[:lr-lp] = [] |
|
302 | self.input_hist_raw[:lr-lp] = [] | |
295 | elif lr < lp: |
|
303 | elif lr < lp: | |
296 | self.input_hist_parsed[:lp-lr] = [] |
|
304 | self.input_hist_parsed[:lp-lr] = [] | |
297 |
|
305 | |||
298 | def reset(self): |
|
306 | def reset(self, new_session=True): | |
299 | """Clear all histories managed by this object, and start a new |
|
307 | """Clear the current session's history, and (optionally) start a new | |
300 | session.""" |
|
308 | session.""" | |
301 | self.input_hist_parsed[:] = [""] |
|
309 | self.input_hist_parsed[:] = [""] | |
302 | self.input_hist_raw[:] = [""] |
|
310 | self.input_hist_raw[:] = [""] | |
303 | self.output_hist.clear() |
|
311 | self.output_hist.clear() | |
304 | # The directory history can't be completely empty |
|
312 | # The directory history can't be completely empty | |
305 | self.dir_hist[:] = [os.getcwd()] |
|
313 | self.dir_hist[:] = [os.getcwd()] | |
306 |
|
314 | |||
|
315 | if new_session: | |||
307 | self.writeout_cache() |
|
316 | self.writeout_cache() | |
308 |
self.init_db() # |
|
317 | self.init_db() # Get new session number | |
309 |
|
318 | |||
310 | # To match, e.g. ~5/8-~2/3 |
|
319 | # To match, e.g. ~5/8-~2/3 | |
311 | range_re = re.compile(r""" |
|
320 | range_re = re.compile(r""" | |
312 | ((?P<startsess>~?\d+)/)? |
|
321 | ((?P<startsess>~?\d+)/)? | |
313 | (?P<start>\d+) # Only the start line num is compulsory |
|
322 | (?P<start>\d+) # Only the start line num is compulsory | |
314 | ((?P<sep>[\-:]) |
|
323 | ((?P<sep>[\-:]) | |
315 | ((?P<endsess>~?\d+)/)? |
|
324 | ((?P<endsess>~?\d+)/)? | |
316 | (?P<end>\d+))? |
|
325 | (?P<end>\d+))? | |
317 | """, re.VERBOSE) |
|
326 | """, re.VERBOSE) | |
318 |
|
327 | |||
319 | def extract_hist_ranges(ranges_str): |
|
328 | def extract_hist_ranges(ranges_str): | |
320 | """Turn a string of history ranges into 3-tuples of (session, start, stop). |
|
329 | """Turn a string of history ranges into 3-tuples of (session, start, stop). | |
321 |
|
330 | |||
322 | Examples |
|
331 | Examples | |
323 | -------- |
|
332 | -------- | |
324 | list(extract_input_ranges("~8/5-~7/4 2")) |
|
333 | list(extract_input_ranges("~8/5-~7/4 2")) | |
325 | [(-8, 5, None), (-7, 1, 4), (0, 2, 3)] |
|
334 | [(-8, 5, None), (-7, 1, 4), (0, 2, 3)] | |
326 | """ |
|
335 | """ | |
327 | for range_str in ranges_str.split(): |
|
336 | for range_str in ranges_str.split(): | |
328 | rmatch = range_re.match(range_str) |
|
337 | rmatch = range_re.match(range_str) | |
329 | start = int(rmatch.group("start")) |
|
338 | start = int(rmatch.group("start")) | |
330 | end = rmatch.group("end") |
|
339 | end = rmatch.group("end") | |
331 | end = int(end) if end else start+1 # If no end specified, get (a, a+1) |
|
340 | end = int(end) if end else start+1 # If no end specified, get (a, a+1) | |
332 | if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3] |
|
341 | if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3] | |
333 | end += 1 |
|
342 | end += 1 | |
334 | startsess = rmatch.group("startsess") or "0" |
|
343 | startsess = rmatch.group("startsess") or "0" | |
335 | endsess = rmatch.group("endsess") or startsess |
|
344 | endsess = rmatch.group("endsess") or startsess | |
336 | startsess = int(startsess.replace("~","-")) |
|
345 | startsess = int(startsess.replace("~","-")) | |
337 | endsess = int(endsess.replace("~","-")) |
|
346 | endsess = int(endsess.replace("~","-")) | |
338 | assert endsess >= startsess |
|
347 | assert endsess >= startsess | |
339 |
|
348 | |||
340 | if endsess == startsess: |
|
349 | if endsess == startsess: | |
341 | yield (startsess, start, end) |
|
350 | yield (startsess, start, end) | |
342 | continue |
|
351 | continue | |
343 | # Multiple sessions in one range: |
|
352 | # Multiple sessions in one range: | |
344 | yield (startsess, start, None) |
|
353 | yield (startsess, start, None) | |
345 | for sess in range(startsess+1, endsess): |
|
354 | for sess in range(startsess+1, endsess): | |
346 | yield (sess, 1, None) |
|
355 | yield (sess, 1, None) | |
347 | yield (endsess, 1, end) |
|
356 | yield (endsess, 1, end) | |
348 |
|
357 | |||
349 | def _format_lineno(session, line): |
|
358 | def _format_lineno(session, line): | |
350 | """Helper function to format line numbers properly.""" |
|
359 | """Helper function to format line numbers properly.""" | |
351 | if session == 0: |
|
360 | if session == 0: | |
352 | return str(line) |
|
361 | return str(line) | |
353 | return "%s#%s" % (session, line) |
|
362 | return "%s#%s" % (session, line) | |
354 |
|
363 | |||
355 | @testdec.skip_doctest |
|
364 | @testdec.skip_doctest | |
356 | def magic_history(self, parameter_s = ''): |
|
365 | def magic_history(self, parameter_s = ''): | |
357 | """Print input history (_i<n> variables), with most recent last. |
|
366 | """Print input history (_i<n> variables), with most recent last. | |
358 |
|
367 | |||
359 | %history -> print at most 40 inputs (some may be multi-line)\\ |
|
368 | %history -> print at most 40 inputs (some may be multi-line)\\ | |
360 | %history n -> print at most n inputs\\ |
|
369 | %history n -> print at most n inputs\\ | |
361 | %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
370 | %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ | |
362 |
|
371 | |||
363 | By default, input history is printed without line numbers so it can be |
|
372 | By default, input history is printed without line numbers so it can be | |
364 | directly pasted into an editor. |
|
373 | directly pasted into an editor. | |
365 |
|
374 | |||
366 | With -n, each input's number <n> is shown, and is accessible as the |
|
375 | With -n, each input's number <n> is shown, and is accessible as the | |
367 | automatically generated variable _i<n> as well as In[<n>]. Multi-line |
|
376 | automatically generated variable _i<n> as well as In[<n>]. Multi-line | |
368 | statements are printed starting at a new line for easy copy/paste. |
|
377 | statements are printed starting at a new line for easy copy/paste. | |
369 |
|
378 | |||
370 | Options: |
|
379 | Options: | |
371 |
|
380 | |||
372 | -n: print line numbers for each input. |
|
381 | -n: print line numbers for each input. | |
373 | This feature is only available if numbered prompts are in use. |
|
382 | This feature is only available if numbered prompts are in use. | |
374 |
|
383 | |||
375 | -o: also print outputs for each input. |
|
384 | -o: also print outputs for each input. | |
376 |
|
385 | |||
377 | -p: print classic '>>>' python prompts before each input. This is useful |
|
386 | -p: print classic '>>>' python prompts before each input. This is useful | |
378 | for making documentation, and in conjunction with -o, for producing |
|
387 | for making documentation, and in conjunction with -o, for producing | |
379 | doctest-ready output. |
|
388 | doctest-ready output. | |
380 |
|
389 | |||
381 | -r: (default) print the 'raw' history, i.e. the actual commands you typed. |
|
390 | -r: (default) print the 'raw' history, i.e. the actual commands you typed. | |
382 |
|
391 | |||
383 | -t: print the 'translated' history, as IPython understands it. IPython |
|
392 | -t: print the 'translated' history, as IPython understands it. IPython | |
384 | filters your input and converts it all into valid Python source before |
|
393 | filters your input and converts it all into valid Python source before | |
385 | executing it (things like magics or aliases are turned into function |
|
394 | executing it (things like magics or aliases are turned into function | |
386 | calls, for example). With this option, you'll see the native history |
|
395 | calls, for example). With this option, you'll see the native history | |
387 | instead of the user-entered version: '%cd /' will be seen as |
|
396 | instead of the user-entered version: '%cd /' will be seen as | |
388 | 'get_ipython().magic("%cd /")' instead of '%cd /'. |
|
397 | 'get_ipython().magic("%cd /")' instead of '%cd /'. | |
389 |
|
398 | |||
390 | -g: treat the arg as a pattern to grep for in (full) history. |
|
399 | -g: treat the arg as a pattern to grep for in (full) history. | |
391 | This includes the saved history (almost all commands ever written). |
|
400 | This includes the saved history (almost all commands ever written). | |
392 | Use '%hist -g' to show full saved history (may be very long). |
|
401 | Use '%hist -g' to show full saved history (may be very long). | |
393 |
|
402 | |||
394 | -l: get the last n lines from all sessions. Specify n as a single arg, or |
|
403 | -l: get the last n lines from all sessions. Specify n as a single arg, or | |
395 | the default is the last 10 lines. |
|
404 | the default is the last 10 lines. | |
396 |
|
405 | |||
397 | -f FILENAME: instead of printing the output to the screen, redirect it to |
|
406 | -f FILENAME: instead of printing the output to the screen, redirect it to | |
398 | the given file. The file is always overwritten, though IPython asks for |
|
407 | the given file. The file is always overwritten, though IPython asks for | |
399 | confirmation first if it already exists. |
|
408 | confirmation first if it already exists. | |
400 |
|
409 | |||
401 | Examples |
|
410 | Examples | |
402 | -------- |
|
411 | -------- | |
403 | :: |
|
412 | :: | |
404 |
|
413 | |||
405 | In [6]: %hist -n 4 6 |
|
414 | In [6]: %hist -n 4 6 | |
406 | 4:a = 12 |
|
415 | 4:a = 12 | |
407 | 5:print a**2 |
|
416 | 5:print a**2 | |
408 |
|
417 | |||
409 | """ |
|
418 | """ | |
410 |
|
419 | |||
411 | if not self.shell.displayhook.do_full_cache: |
|
420 | if not self.shell.displayhook.do_full_cache: | |
412 | print('This feature is only available if numbered prompts are in use.') |
|
421 | print('This feature is only available if numbered prompts are in use.') | |
413 | return |
|
422 | return | |
414 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') |
|
423 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') | |
415 |
|
424 | |||
416 | # For brevity |
|
425 | # For brevity | |
417 | history_manager = self.shell.history_manager |
|
426 | history_manager = self.shell.history_manager | |
418 |
|
427 | |||
419 | # Check if output to specific file was requested. |
|
428 | # Check if output to specific file was requested. | |
420 | try: |
|
429 | try: | |
421 | outfname = opts['f'] |
|
430 | outfname = opts['f'] | |
422 | except KeyError: |
|
431 | except KeyError: | |
423 | outfile = IPython.utils.io.Term.cout # default |
|
432 | outfile = IPython.utils.io.Term.cout # default | |
424 | # We don't want to close stdout at the end! |
|
433 | # We don't want to close stdout at the end! | |
425 | close_at_end = False |
|
434 | close_at_end = False | |
426 | else: |
|
435 | else: | |
427 | if os.path.exists(outfname): |
|
436 | if os.path.exists(outfname): | |
428 | if not ask_yes_no("File %r exists. Overwrite?" % outfname): |
|
437 | if not ask_yes_no("File %r exists. Overwrite?" % outfname): | |
429 | print('Aborting.') |
|
438 | print('Aborting.') | |
430 | return |
|
439 | return | |
431 |
|
440 | |||
432 | outfile = open(outfname,'w') |
|
441 | outfile = open(outfname,'w') | |
433 | close_at_end = True |
|
442 | close_at_end = True | |
434 |
|
443 | |||
435 | print_nums = 'n' in opts |
|
444 | print_nums = 'n' in opts | |
436 | get_output = 'o' in opts |
|
445 | get_output = 'o' in opts | |
437 | pyprompts = 'p' in opts |
|
446 | pyprompts = 'p' in opts | |
438 | # Raw history is the default |
|
447 | # Raw history is the default | |
439 | raw = not('t' in opts) |
|
448 | raw = not('t' in opts) | |
440 |
|
449 | |||
441 | default_length = 40 |
|
450 | default_length = 40 | |
442 | pattern = None |
|
451 | pattern = None | |
443 |
|
452 | |||
444 | # Glob search: |
|
453 | # Glob search: | |
445 | if 'g' in opts: |
|
454 | if 'g' in opts: | |
446 | pattern = "*" + args + "*" if args else "*" |
|
455 | pattern = "*" + args + "*" if args else "*" | |
447 |
|
456 | |||
448 | # Display: |
|
457 | # Display: | |
449 | matches_current_session = [] |
|
458 | matches_current_session = [] | |
450 | for session, line, s in history_manager.get_hist_search(pattern, raw): |
|
459 | for session, line, s in history_manager.get_hist_search(pattern, raw): | |
451 | if session == history_manager.session_number: |
|
460 | if session == history_manager.session_number: | |
452 | matches_current_session.append(line, s) |
|
461 | matches_current_session.append(line, s) | |
453 | continue |
|
462 | continue | |
454 | print("%d#%d: %s" %(session, line, s.expandtabs(4)), file=outfile) |
|
463 | print("%d#%d: %s" %(session, line, s.expandtabs(4)), file=outfile) | |
455 | if matches_current_session: |
|
464 | if matches_current_session: | |
456 | print("=== Current session: ===", file=outfile) |
|
465 | print("=== Current session: ===", file=outfile) | |
457 | for line, s in matches_current_session: |
|
466 | for line, s in matches_current_session: | |
458 | print("%d: %s" %(line, s.expandtabs(4)), file=outfile) |
|
467 | print("%d: %s" %(line, s.expandtabs(4)), file=outfile) | |
459 | return |
|
468 | return | |
460 |
|
469 | |||
461 | if 'l' in opts: # Get 'tail' |
|
470 | if 'l' in opts: # Get 'tail' | |
462 | try: |
|
471 | try: | |
463 | n = int(args) |
|
472 | n = int(args) | |
464 | except ValueError, IndexError: |
|
473 | except ValueError, IndexError: | |
465 | n = 10 |
|
474 | n = 10 | |
466 | hist = history_manager.get_hist_tail(n, raw=raw) |
|
475 | hist = history_manager.get_hist_tail(n, raw=raw, output=get_output) | |
467 | else: |
|
476 | else: | |
468 | if args: # Get history by ranges |
|
477 | if args: # Get history by ranges | |
469 | hist = history_manager.get_hist_from_rangestr(args, raw, get_output) |
|
478 | hist = history_manager.get_hist_from_rangestr(args, raw, get_output) | |
470 | else: # Just get history for the current session |
|
479 | else: # Just get history for the current session | |
471 | hist = history_manager.get_history(raw=raw, output=get_output) |
|
480 | hist = history_manager.get_history(raw=raw, output=get_output) | |
472 | # Pull hist into a list, so we can get the widest number in it. |
|
481 | # Pull hist into a list, so we can get the widest number in it. | |
473 | hist = list(hist) |
|
482 | hist = list(hist) | |
|
483 | if not hist: | |||
|
484 | return | |||
474 |
|
485 | |||
475 | width = max(len(_format_lineno(s, l)) for s, l, _ in hist) |
|
486 | width = max(len(_format_lineno(s, l)) for s, l, _ in hist) | |
476 |
|
487 | |||
477 | for session, lineno, inline in hist: |
|
488 | for session, lineno, inline in hist: | |
478 | # Print user history with tabs expanded to 4 spaces. The GUI clients |
|
489 | # Print user history with tabs expanded to 4 spaces. The GUI clients | |
479 | # use hard tabs for easier usability in auto-indented code, but we want |
|
490 | # use hard tabs for easier usability in auto-indented code, but we want | |
480 | # to produce PEP-8 compliant history for safe pasting into an editor. |
|
491 | # to produce PEP-8 compliant history for safe pasting into an editor. | |
481 | if get_output: |
|
492 | if get_output: | |
482 | inline, output = inline |
|
493 | inline, output = inline | |
483 | inline = inline.expandtabs(4).rstrip() |
|
494 | inline = inline.expandtabs(4).rstrip() | |
484 |
|
495 | |||
485 | if pattern is not None and not fnmatch.fnmatch(inline, pattern): |
|
496 | if pattern is not None and not fnmatch.fnmatch(inline, pattern): | |
486 | continue |
|
497 | continue | |
487 |
|
498 | |||
488 | multiline = "\n" in inline |
|
499 | multiline = "\n" in inline | |
489 | line_sep = '\n' if multiline else '' |
|
500 | line_sep = '\n' if multiline else '' | |
490 | if print_nums: |
|
501 | if print_nums: | |
491 | print('%s:%s' % (_format_lineno(session, lineno).ljust(width), |
|
502 | print('%s:%s' % (_format_lineno(session, lineno).ljust(width), | |
492 | line_sep), file=outfile, end='') |
|
503 | line_sep), file=outfile, end='') | |
493 | if pyprompts: |
|
504 | if pyprompts: | |
494 | print(">>> ", end="", file=outfile) |
|
505 | print(">>> ", end="", file=outfile) | |
495 | if multiline: |
|
506 | if multiline: | |
496 | inline = "\n... ".join(inline.splitlines()) + "\n..." |
|
507 | inline = "\n... ".join(inline.splitlines()) + "\n..." | |
497 | print(inline, file=outfile) |
|
508 | print(inline, file=outfile) | |
498 | if get_output and output: |
|
509 | if get_output and output: | |
499 |
print( |
|
510 | print(output, file=outfile) | |
500 |
|
511 | |||
501 | if close_at_end: |
|
512 | if close_at_end: | |
502 | outfile.close() |
|
513 | outfile.close() | |
503 |
|
514 | |||
504 | # %hist is an alternative name |
|
515 | # %hist is an alternative name | |
505 | magic_hist = magic_history |
|
516 | magic_hist = magic_history | |
506 |
|
517 | |||
507 |
|
518 | |||
508 | def rep_f(self, arg): |
|
519 | def rep_f(self, arg): | |
509 | r""" Repeat a command, or get command to input line for editing |
|
520 | r""" Repeat a command, or get command to input line for editing | |
510 |
|
521 | |||
511 | - %rep (no arguments): |
|
522 | - %rep (no arguments): | |
512 |
|
523 | |||
513 | Place a string version of last computation result (stored in the special '_' |
|
524 | Place a string version of last computation result (stored in the special '_' | |
514 | variable) to the next input prompt. Allows you to create elaborate command |
|
525 | variable) to the next input prompt. Allows you to create elaborate command | |
515 | lines without using copy-paste:: |
|
526 | lines without using copy-paste:: | |
516 |
|
527 | |||
517 | $ l = ["hei", "vaan"] |
|
528 | $ l = ["hei", "vaan"] | |
518 | $ "".join(l) |
|
529 | $ "".join(l) | |
519 | ==> heivaan |
|
530 | ==> heivaan | |
520 | $ %rep |
|
531 | $ %rep | |
521 | $ heivaan_ <== cursor blinking |
|
532 | $ heivaan_ <== cursor blinking | |
522 |
|
533 | |||
523 | %rep 45 |
|
534 | %rep 45 | |
524 |
|
535 | |||
525 | Place history line 45 to next input prompt. Use %hist to find out the |
|
536 | Place history line 45 to next input prompt. Use %hist to find out the | |
526 | number. |
|
537 | number. | |
527 |
|
538 | |||
528 | %rep 1-4 6-7 3 |
|
539 | %rep 1-4 6-7 3 | |
529 |
|
540 | |||
530 | Repeat the specified lines immediately. Input slice syntax is the same as |
|
541 | Repeat the specified lines immediately. Input slice syntax is the same as | |
531 | in %macro and %save. |
|
542 | in %macro and %save. | |
532 |
|
543 | |||
533 | %rep foo |
|
544 | %rep foo | |
534 |
|
545 | |||
535 | Place the most recent line that has the substring "foo" to next input. |
|
546 | Place the most recent line that has the substring "foo" to next input. | |
536 | (e.g. 'svn ci -m foobar'). |
|
547 | (e.g. 'svn ci -m foobar'). | |
537 | """ |
|
548 | """ | |
538 |
|
549 | |||
539 | opts,args = self.parse_options(arg,'',mode='list') |
|
550 | opts,args = self.parse_options(arg,'',mode='list') | |
540 | if not args: |
|
551 | if not args: | |
541 | self.set_next_input(str(self.shell.user_ns["_"])) |
|
552 | self.set_next_input(str(self.shell.user_ns["_"])) | |
542 | return |
|
553 | return | |
543 |
|
554 | |||
544 | if len(args) == 1 and not '-' in args[0]: |
|
555 | if len(args) == 1 and not '-' in args[0]: | |
545 | arg = args[0] |
|
556 | arg = args[0] | |
546 | if len(arg) > 1 and arg.startswith('0'): |
|
557 | if len(arg) > 1 and arg.startswith('0'): | |
547 | # get from shadow hist |
|
558 | # get from shadow hist | |
548 | num = int(arg[1:]) |
|
559 | num = int(arg[1:]) | |
549 | line = self.shell.shadowhist.get(num) |
|
560 | line = self.shell.shadowhist.get(num) | |
550 | self.set_next_input(str(line)) |
|
561 | self.set_next_input(str(line)) | |
551 | return |
|
562 | return | |
552 | try: |
|
563 | try: | |
553 | num = int(args[0]) |
|
564 | num = int(args[0]) | |
554 | self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip()) |
|
565 | self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip()) | |
555 | return |
|
566 | return | |
556 | except ValueError: |
|
567 | except ValueError: | |
557 | pass |
|
568 | pass | |
558 |
|
569 | |||
559 | for h in reversed(self.shell.input_hist_raw): |
|
570 | for h in reversed(self.shell.input_hist_raw): | |
560 | if 'rep' in h: |
|
571 | if 'rep' in h: | |
561 | continue |
|
572 | continue | |
562 | if fnmatch.fnmatch(h,'*' + arg + '*'): |
|
573 | if fnmatch.fnmatch(h,'*' + arg + '*'): | |
563 | self.set_next_input(str(h).rstrip()) |
|
574 | self.set_next_input(str(h).rstrip()) | |
564 | return |
|
575 | return | |
565 |
|
576 | |||
566 | try: |
|
577 | try: | |
567 | lines = self.extract_input_slices(args, True) |
|
578 | lines = self.extract_input_slices(args, True) | |
568 | print("lines", lines) |
|
579 | print("lines", lines) | |
569 | self.run_cell(lines) |
|
580 | self.run_cell(lines) | |
570 | except ValueError: |
|
581 | except ValueError: | |
571 | print("Not found in recent history:", args) |
|
582 | print("Not found in recent history:", args) | |
572 |
|
583 | |||
573 |
|
584 | |||
574 | def init_ipython(ip): |
|
585 | def init_ipython(ip): | |
575 | ip.define_magic("rep",rep_f) |
|
586 | ip.define_magic("rep",rep_f) | |
576 | ip.define_magic("hist",magic_hist) |
|
587 | ip.define_magic("hist",magic_hist) | |
577 | ip.define_magic("history",magic_history) |
|
588 | ip.define_magic("history",magic_history) | |
578 |
|
589 | |||
579 | # XXX - ipy_completers are in quarantine, need to be updated to new apis |
|
590 | # XXX - ipy_completers are in quarantine, need to be updated to new apis | |
580 | #import ipy_completers |
|
591 | #import ipy_completers | |
581 | #ipy_completers.quick_completer('%hist' ,'-g -t -r -n') |
|
592 | #ipy_completers.quick_completer('%hist' ,'-g -t -r -n') |
@@ -1,2549 +1,2547 b'' | |||||
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-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 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 os |
|
25 | import os | |
26 | import re |
|
26 | import re | |
27 | import sys |
|
27 | import sys | |
28 | import tempfile |
|
28 | import tempfile | |
29 | import types |
|
29 | import types | |
30 | from contextlib import nested |
|
30 | from contextlib import nested | |
31 |
|
31 | |||
32 | from IPython.config.configurable import Configurable |
|
32 | from IPython.config.configurable import Configurable | |
33 | from IPython.core import debugger, oinspect |
|
33 | from IPython.core import debugger, oinspect | |
34 | from IPython.core import history as ipcorehist |
|
34 | from IPython.core import history as ipcorehist | |
35 | from IPython.core import page |
|
35 | from IPython.core import page | |
36 | from IPython.core import prefilter |
|
36 | from IPython.core import prefilter | |
37 | from IPython.core import shadowns |
|
37 | from IPython.core import shadowns | |
38 | from IPython.core import ultratb |
|
38 | from IPython.core import ultratb | |
39 | from IPython.core.alias import AliasManager |
|
39 | from IPython.core.alias import AliasManager | |
40 | from IPython.core.builtin_trap import BuiltinTrap |
|
40 | from IPython.core.builtin_trap import BuiltinTrap | |
41 | from IPython.core.compilerop import CachingCompiler |
|
41 | from IPython.core.compilerop import CachingCompiler | |
42 | from IPython.core.display_trap import DisplayTrap |
|
42 | from IPython.core.display_trap import DisplayTrap | |
43 | from IPython.core.displayhook import DisplayHook |
|
43 | from IPython.core.displayhook import DisplayHook | |
44 | from IPython.core.displaypub import DisplayPublisher |
|
44 | from IPython.core.displaypub import DisplayPublisher | |
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.formatters import DisplayFormatter |
|
48 | from IPython.core.formatters import DisplayFormatter | |
49 | from IPython.core.history import HistoryManager |
|
49 | from IPython.core.history import HistoryManager | |
50 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
50 | from IPython.core.inputsplitter import IPythonInputSplitter | |
51 | from IPython.core.logger import Logger |
|
51 | from IPython.core.logger import Logger | |
52 | from IPython.core.magic import Magic |
|
52 | from IPython.core.magic import Magic | |
53 | from IPython.core.payload import PayloadManager |
|
53 | from IPython.core.payload import PayloadManager | |
54 | from IPython.core.plugin import PluginManager |
|
54 | from IPython.core.plugin import PluginManager | |
55 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC |
|
55 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC | |
56 | from IPython.external.Itpl import ItplNS |
|
56 | from IPython.external.Itpl import ItplNS | |
57 | from IPython.utils import PyColorize |
|
57 | from IPython.utils import PyColorize | |
58 | from IPython.utils import io |
|
58 | from IPython.utils import io | |
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.pickleshare import PickleShareDB |
|
63 | from IPython.utils.pickleshare import PickleShareDB | |
64 | from IPython.utils.process import system, getoutput |
|
64 | from IPython.utils.process import system, getoutput | |
65 | from IPython.utils.strdispatch import StrDispatch |
|
65 | from IPython.utils.strdispatch import StrDispatch | |
66 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
66 | from IPython.utils.syspathcontext import prepended_to_syspath | |
67 | from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList |
|
67 | from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList | |
68 | from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum, |
|
68 | from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum, | |
69 | List, Unicode, Instance, Type) |
|
69 | List, Unicode, Instance, Type) | |
70 | from IPython.utils.warn import warn, error, fatal |
|
70 | from IPython.utils.warn import warn, error, fatal | |
71 | import IPython.core.hooks |
|
71 | import IPython.core.hooks | |
72 |
|
72 | |||
73 | #----------------------------------------------------------------------------- |
|
73 | #----------------------------------------------------------------------------- | |
74 | # Globals |
|
74 | # Globals | |
75 | #----------------------------------------------------------------------------- |
|
75 | #----------------------------------------------------------------------------- | |
76 |
|
76 | |||
77 | # compiled regexps for autoindent management |
|
77 | # compiled regexps for autoindent management | |
78 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
78 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
79 |
|
79 | |||
80 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
81 | # Utilities |
|
81 | # Utilities | |
82 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
83 |
|
83 | |||
84 | # store the builtin raw_input globally, and use this always, in case user code |
|
84 | # store the builtin raw_input globally, and use this always, in case user code | |
85 | # overwrites it (like wx.py.PyShell does) |
|
85 | # overwrites it (like wx.py.PyShell does) | |
86 | raw_input_original = raw_input |
|
86 | raw_input_original = raw_input | |
87 |
|
87 | |||
88 | def softspace(file, newvalue): |
|
88 | def softspace(file, newvalue): | |
89 | """Copied from code.py, to remove the dependency""" |
|
89 | """Copied from code.py, to remove the dependency""" | |
90 |
|
90 | |||
91 | oldvalue = 0 |
|
91 | oldvalue = 0 | |
92 | try: |
|
92 | try: | |
93 | oldvalue = file.softspace |
|
93 | oldvalue = file.softspace | |
94 | except AttributeError: |
|
94 | except AttributeError: | |
95 | pass |
|
95 | pass | |
96 | try: |
|
96 | try: | |
97 | file.softspace = newvalue |
|
97 | file.softspace = newvalue | |
98 | except (AttributeError, TypeError): |
|
98 | except (AttributeError, TypeError): | |
99 | # "attribute-less object" or "read-only attributes" |
|
99 | # "attribute-less object" or "read-only attributes" | |
100 | pass |
|
100 | pass | |
101 | return oldvalue |
|
101 | return oldvalue | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | def no_op(*a, **kw): pass |
|
104 | def no_op(*a, **kw): pass | |
105 |
|
105 | |||
106 | class SpaceInInput(Exception): pass |
|
106 | class SpaceInInput(Exception): pass | |
107 |
|
107 | |||
108 | class Bunch: pass |
|
108 | class Bunch: pass | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | def get_default_colors(): |
|
111 | def get_default_colors(): | |
112 | if sys.platform=='darwin': |
|
112 | if sys.platform=='darwin': | |
113 | return "LightBG" |
|
113 | return "LightBG" | |
114 | elif os.name=='nt': |
|
114 | elif os.name=='nt': | |
115 | return 'Linux' |
|
115 | return 'Linux' | |
116 | else: |
|
116 | else: | |
117 | return 'Linux' |
|
117 | return 'Linux' | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | class SeparateStr(Str): |
|
120 | class SeparateStr(Str): | |
121 | """A Str subclass to validate separate_in, separate_out, etc. |
|
121 | """A Str subclass to validate separate_in, separate_out, etc. | |
122 |
|
122 | |||
123 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. |
|
123 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. | |
124 | """ |
|
124 | """ | |
125 |
|
125 | |||
126 | def validate(self, obj, value): |
|
126 | def validate(self, obj, value): | |
127 | if value == '0': value = '' |
|
127 | if value == '0': value = '' | |
128 | value = value.replace('\\n','\n') |
|
128 | value = value.replace('\\n','\n') | |
129 | return super(SeparateStr, self).validate(obj, value) |
|
129 | return super(SeparateStr, self).validate(obj, value) | |
130 |
|
130 | |||
131 | class MultipleInstanceError(Exception): |
|
131 | class MultipleInstanceError(Exception): | |
132 | pass |
|
132 | pass | |
133 |
|
133 | |||
134 |
|
134 | |||
135 | #----------------------------------------------------------------------------- |
|
135 | #----------------------------------------------------------------------------- | |
136 | # Main IPython class |
|
136 | # Main IPython class | |
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 | display_formatter = Instance(DisplayFormatter) |
|
154 | display_formatter = Instance(DisplayFormatter) | |
155 | displayhook_class = Type(DisplayHook) |
|
155 | displayhook_class = Type(DisplayHook) | |
156 | display_pub_class = Type(DisplayPublisher) |
|
156 | display_pub_class = Type(DisplayPublisher) | |
157 |
|
157 | |||
158 | exit_now = CBool(False) |
|
158 | exit_now = CBool(False) | |
159 | # Monotonically increasing execution counter |
|
159 | # Monotonically increasing execution counter | |
160 | execution_count = Int(1) |
|
160 | execution_count = Int(1) | |
161 | filename = Str("<ipython console>") |
|
161 | filename = Str("<ipython console>") | |
162 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
162 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | |
163 |
|
163 | |||
164 | # Input splitter, to split entire cells of input into either individual |
|
164 | # Input splitter, to split entire cells of input into either individual | |
165 | # interactive statements or whole blocks. |
|
165 | # interactive statements or whole blocks. | |
166 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
166 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
167 | (), {}) |
|
167 | (), {}) | |
168 | logstart = CBool(False, config=True) |
|
168 | logstart = CBool(False, config=True) | |
169 | logfile = Str('', config=True) |
|
169 | logfile = Str('', config=True) | |
170 | logappend = Str('', config=True) |
|
170 | logappend = Str('', config=True) | |
171 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
171 | object_info_string_level = Enum((0,1,2), default_value=0, | |
172 | config=True) |
|
172 | config=True) | |
173 | pdb = CBool(False, config=True) |
|
173 | pdb = CBool(False, config=True) | |
174 |
|
174 | |||
175 | profile = Str('', config=True) |
|
175 | profile = Str('', config=True) | |
176 | prompt_in1 = Str('In [\\#]: ', config=True) |
|
176 | prompt_in1 = Str('In [\\#]: ', config=True) | |
177 | prompt_in2 = Str(' .\\D.: ', config=True) |
|
177 | prompt_in2 = Str(' .\\D.: ', config=True) | |
178 | prompt_out = Str('Out[\\#]: ', config=True) |
|
178 | prompt_out = Str('Out[\\#]: ', config=True) | |
179 | prompts_pad_left = CBool(True, config=True) |
|
179 | prompts_pad_left = CBool(True, config=True) | |
180 | quiet = CBool(False, config=True) |
|
180 | quiet = CBool(False, config=True) | |
181 |
|
181 | |||
182 | history_length = Int(10000, config=True) |
|
182 | history_length = Int(10000, config=True) | |
183 |
|
183 | |||
184 | # The readline stuff will eventually be moved to the terminal subclass |
|
184 | # The readline stuff will eventually be moved to the terminal subclass | |
185 | # but for now, we can't do that as readline is welded in everywhere. |
|
185 | # but for now, we can't do that as readline is welded in everywhere. | |
186 | readline_use = CBool(True, config=True) |
|
186 | readline_use = CBool(True, config=True) | |
187 | readline_merge_completions = CBool(True, config=True) |
|
187 | readline_merge_completions = CBool(True, config=True) | |
188 | readline_omit__names = Enum((0,1,2), default_value=2, config=True) |
|
188 | readline_omit__names = Enum((0,1,2), default_value=2, config=True) | |
189 | readline_remove_delims = Str('-/~', config=True) |
|
189 | readline_remove_delims = Str('-/~', config=True) | |
190 | readline_parse_and_bind = List([ |
|
190 | readline_parse_and_bind = List([ | |
191 | 'tab: complete', |
|
191 | 'tab: complete', | |
192 | '"\C-l": clear-screen', |
|
192 | '"\C-l": clear-screen', | |
193 | 'set show-all-if-ambiguous on', |
|
193 | 'set show-all-if-ambiguous on', | |
194 | '"\C-o": tab-insert', |
|
194 | '"\C-o": tab-insert', | |
195 | '"\M-i": " "', |
|
195 | '"\M-i": " "', | |
196 | '"\M-o": "\d\d\d\d"', |
|
196 | '"\M-o": "\d\d\d\d"', | |
197 | '"\M-I": "\d\d\d\d"', |
|
197 | '"\M-I": "\d\d\d\d"', | |
198 | '"\C-r": reverse-search-history', |
|
198 | '"\C-r": reverse-search-history', | |
199 | '"\C-s": forward-search-history', |
|
199 | '"\C-s": forward-search-history', | |
200 | '"\C-p": history-search-backward', |
|
200 | '"\C-p": history-search-backward', | |
201 | '"\C-n": history-search-forward', |
|
201 | '"\C-n": history-search-forward', | |
202 | '"\e[A": history-search-backward', |
|
202 | '"\e[A": history-search-backward', | |
203 | '"\e[B": history-search-forward', |
|
203 | '"\e[B": history-search-forward', | |
204 | '"\C-k": kill-line', |
|
204 | '"\C-k": kill-line', | |
205 | '"\C-u": unix-line-discard', |
|
205 | '"\C-u": unix-line-discard', | |
206 | ], allow_none=False, config=True) |
|
206 | ], allow_none=False, config=True) | |
207 |
|
207 | |||
208 | # TODO: this part of prompt management should be moved to the frontends. |
|
208 | # TODO: this part of prompt management should be moved to the frontends. | |
209 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
209 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
210 | separate_in = SeparateStr('\n', config=True) |
|
210 | separate_in = SeparateStr('\n', config=True) | |
211 | separate_out = SeparateStr('', config=True) |
|
211 | separate_out = SeparateStr('', config=True) | |
212 | separate_out2 = SeparateStr('', config=True) |
|
212 | separate_out2 = SeparateStr('', config=True) | |
213 | wildcards_case_sensitive = CBool(True, config=True) |
|
213 | wildcards_case_sensitive = CBool(True, config=True) | |
214 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
214 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
215 | default_value='Context', config=True) |
|
215 | default_value='Context', config=True) | |
216 |
|
216 | |||
217 | # Subcomponents of InteractiveShell |
|
217 | # Subcomponents of InteractiveShell | |
218 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
218 | alias_manager = Instance('IPython.core.alias.AliasManager') | |
219 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
219 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | |
220 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
220 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | |
221 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
221 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') | |
222 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
222 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') | |
223 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
223 | plugin_manager = Instance('IPython.core.plugin.PluginManager') | |
224 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
224 | payload_manager = Instance('IPython.core.payload.PayloadManager') | |
225 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
225 | history_manager = Instance('IPython.core.history.HistoryManager') | |
226 |
|
226 | |||
227 | # Private interface |
|
227 | # Private interface | |
228 | _post_execute = set() |
|
228 | _post_execute = set() | |
229 |
|
229 | |||
230 | def __init__(self, config=None, ipython_dir=None, |
|
230 | def __init__(self, config=None, ipython_dir=None, | |
231 | user_ns=None, user_global_ns=None, |
|
231 | user_ns=None, user_global_ns=None, | |
232 | custom_exceptions=((), None)): |
|
232 | custom_exceptions=((), None)): | |
233 |
|
233 | |||
234 | # This is where traits with a config_key argument are updated |
|
234 | # This is where traits with a config_key argument are updated | |
235 | # from the values on config. |
|
235 | # from the values on config. | |
236 | super(InteractiveShell, self).__init__(config=config) |
|
236 | super(InteractiveShell, self).__init__(config=config) | |
237 |
|
237 | |||
238 | # These are relatively independent and stateless |
|
238 | # These are relatively independent and stateless | |
239 | self.init_ipython_dir(ipython_dir) |
|
239 | self.init_ipython_dir(ipython_dir) | |
240 | self.init_instance_attrs() |
|
240 | self.init_instance_attrs() | |
241 | self.init_environment() |
|
241 | self.init_environment() | |
242 |
|
242 | |||
243 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
243 | # Create namespaces (user_ns, user_global_ns, etc.) | |
244 | self.init_create_namespaces(user_ns, user_global_ns) |
|
244 | self.init_create_namespaces(user_ns, user_global_ns) | |
245 | # This has to be done after init_create_namespaces because it uses |
|
245 | # This has to be done after init_create_namespaces because it uses | |
246 | # something in self.user_ns, but before init_sys_modules, which |
|
246 | # something in self.user_ns, but before init_sys_modules, which | |
247 | # is the first thing to modify sys. |
|
247 | # is the first thing to modify sys. | |
248 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
248 | # TODO: When we override sys.stdout and sys.stderr before this class | |
249 | # is created, we are saving the overridden ones here. Not sure if this |
|
249 | # is created, we are saving the overridden ones here. Not sure if this | |
250 | # is what we want to do. |
|
250 | # is what we want to do. | |
251 | self.save_sys_module_state() |
|
251 | self.save_sys_module_state() | |
252 | self.init_sys_modules() |
|
252 | self.init_sys_modules() | |
253 |
|
253 | |||
254 | # While we're trying to have each part of the code directly access what |
|
254 | # While we're trying to have each part of the code directly access what | |
255 | # it needs without keeping redundant references to objects, we have too |
|
255 | # it needs without keeping redundant references to objects, we have too | |
256 | # much legacy code that expects ip.db to exist. |
|
256 | # much legacy code that expects ip.db to exist. | |
257 | self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db')) |
|
257 | self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db')) | |
258 |
|
258 | |||
259 | self.init_history() |
|
259 | self.init_history() | |
260 | self.init_encoding() |
|
260 | self.init_encoding() | |
261 | self.init_prefilter() |
|
261 | self.init_prefilter() | |
262 |
|
262 | |||
263 | Magic.__init__(self, self) |
|
263 | Magic.__init__(self, self) | |
264 |
|
264 | |||
265 | self.init_syntax_highlighting() |
|
265 | self.init_syntax_highlighting() | |
266 | self.init_hooks() |
|
266 | self.init_hooks() | |
267 | self.init_pushd_popd_magic() |
|
267 | self.init_pushd_popd_magic() | |
268 | # self.init_traceback_handlers use to be here, but we moved it below |
|
268 | # self.init_traceback_handlers use to be here, but we moved it below | |
269 | # because it and init_io have to come after init_readline. |
|
269 | # because it and init_io have to come after init_readline. | |
270 | self.init_user_ns() |
|
270 | self.init_user_ns() | |
271 | self.init_logger() |
|
271 | self.init_logger() | |
272 | self.init_alias() |
|
272 | self.init_alias() | |
273 | self.init_builtins() |
|
273 | self.init_builtins() | |
274 |
|
274 | |||
275 | # pre_config_initialization |
|
275 | # pre_config_initialization | |
276 |
|
276 | |||
277 | # The next section should contain everything that was in ipmaker. |
|
277 | # The next section should contain everything that was in ipmaker. | |
278 | self.init_logstart() |
|
278 | self.init_logstart() | |
279 |
|
279 | |||
280 | # The following was in post_config_initialization |
|
280 | # The following was in post_config_initialization | |
281 | self.init_inspector() |
|
281 | self.init_inspector() | |
282 | # init_readline() must come before init_io(), because init_io uses |
|
282 | # init_readline() must come before init_io(), because init_io uses | |
283 | # readline related things. |
|
283 | # readline related things. | |
284 | self.init_readline() |
|
284 | self.init_readline() | |
285 | # init_completer must come after init_readline, because it needs to |
|
285 | # init_completer must come after init_readline, because it needs to | |
286 | # know whether readline is present or not system-wide to configure the |
|
286 | # know whether readline is present or not system-wide to configure the | |
287 | # completers, since the completion machinery can now operate |
|
287 | # completers, since the completion machinery can now operate | |
288 | # independently of readline (e.g. over the network) |
|
288 | # independently of readline (e.g. over the network) | |
289 | self.init_completer() |
|
289 | self.init_completer() | |
290 | # TODO: init_io() needs to happen before init_traceback handlers |
|
290 | # TODO: init_io() needs to happen before init_traceback handlers | |
291 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
291 | # because the traceback handlers hardcode the stdout/stderr streams. | |
292 | # This logic in in debugger.Pdb and should eventually be changed. |
|
292 | # This logic in in debugger.Pdb and should eventually be changed. | |
293 | self.init_io() |
|
293 | self.init_io() | |
294 | self.init_traceback_handlers(custom_exceptions) |
|
294 | self.init_traceback_handlers(custom_exceptions) | |
295 | self.init_prompts() |
|
295 | self.init_prompts() | |
296 | self.init_display_formatter() |
|
296 | self.init_display_formatter() | |
297 | self.init_display_pub() |
|
297 | self.init_display_pub() | |
298 | self.init_displayhook() |
|
298 | self.init_displayhook() | |
299 | self.init_reload_doctest() |
|
299 | self.init_reload_doctest() | |
300 | self.init_magics() |
|
300 | self.init_magics() | |
301 | self.init_pdb() |
|
301 | self.init_pdb() | |
302 | self.init_extension_manager() |
|
302 | self.init_extension_manager() | |
303 | self.init_plugin_manager() |
|
303 | self.init_plugin_manager() | |
304 | self.init_payload() |
|
304 | self.init_payload() | |
305 | self.hooks.late_startup_hook() |
|
305 | self.hooks.late_startup_hook() | |
306 | atexit.register(self.atexit_operations) |
|
306 | atexit.register(self.atexit_operations) | |
307 |
|
307 | |||
308 | @classmethod |
|
308 | @classmethod | |
309 | def instance(cls, *args, **kwargs): |
|
309 | def instance(cls, *args, **kwargs): | |
310 | """Returns a global InteractiveShell instance.""" |
|
310 | """Returns a global InteractiveShell instance.""" | |
311 | if cls._instance is None: |
|
311 | if cls._instance is None: | |
312 | inst = cls(*args, **kwargs) |
|
312 | inst = cls(*args, **kwargs) | |
313 | # Now make sure that the instance will also be returned by |
|
313 | # Now make sure that the instance will also be returned by | |
314 | # the subclasses instance attribute. |
|
314 | # the subclasses instance attribute. | |
315 | for subclass in cls.mro(): |
|
315 | for subclass in cls.mro(): | |
316 | if issubclass(cls, subclass) and \ |
|
316 | if issubclass(cls, subclass) and \ | |
317 | issubclass(subclass, InteractiveShell): |
|
317 | issubclass(subclass, InteractiveShell): | |
318 | subclass._instance = inst |
|
318 | subclass._instance = inst | |
319 | else: |
|
319 | else: | |
320 | break |
|
320 | break | |
321 | if isinstance(cls._instance, cls): |
|
321 | if isinstance(cls._instance, cls): | |
322 | return cls._instance |
|
322 | return cls._instance | |
323 | else: |
|
323 | else: | |
324 | raise MultipleInstanceError( |
|
324 | raise MultipleInstanceError( | |
325 | 'Multiple incompatible subclass instances of ' |
|
325 | 'Multiple incompatible subclass instances of ' | |
326 | 'InteractiveShell are being created.' |
|
326 | 'InteractiveShell are being created.' | |
327 | ) |
|
327 | ) | |
328 |
|
328 | |||
329 | @classmethod |
|
329 | @classmethod | |
330 | def initialized(cls): |
|
330 | def initialized(cls): | |
331 | return hasattr(cls, "_instance") |
|
331 | return hasattr(cls, "_instance") | |
332 |
|
332 | |||
333 | def get_ipython(self): |
|
333 | def get_ipython(self): | |
334 | """Return the currently running IPython instance.""" |
|
334 | """Return the currently running IPython instance.""" | |
335 | return self |
|
335 | return self | |
336 |
|
336 | |||
337 | #------------------------------------------------------------------------- |
|
337 | #------------------------------------------------------------------------- | |
338 | # Trait changed handlers |
|
338 | # Trait changed handlers | |
339 | #------------------------------------------------------------------------- |
|
339 | #------------------------------------------------------------------------- | |
340 |
|
340 | |||
341 | def _ipython_dir_changed(self, name, new): |
|
341 | def _ipython_dir_changed(self, name, new): | |
342 | if not os.path.isdir(new): |
|
342 | if not os.path.isdir(new): | |
343 | os.makedirs(new, mode = 0777) |
|
343 | os.makedirs(new, mode = 0777) | |
344 |
|
344 | |||
345 | def set_autoindent(self,value=None): |
|
345 | def set_autoindent(self,value=None): | |
346 | """Set the autoindent flag, checking for readline support. |
|
346 | """Set the autoindent flag, checking for readline support. | |
347 |
|
347 | |||
348 | If called with no arguments, it acts as a toggle.""" |
|
348 | If called with no arguments, it acts as a toggle.""" | |
349 |
|
349 | |||
350 | if not self.has_readline: |
|
350 | if not self.has_readline: | |
351 | if os.name == 'posix': |
|
351 | if os.name == 'posix': | |
352 | warn("The auto-indent feature requires the readline library") |
|
352 | warn("The auto-indent feature requires the readline library") | |
353 | self.autoindent = 0 |
|
353 | self.autoindent = 0 | |
354 | return |
|
354 | return | |
355 | if value is None: |
|
355 | if value is None: | |
356 | self.autoindent = not self.autoindent |
|
356 | self.autoindent = not self.autoindent | |
357 | else: |
|
357 | else: | |
358 | self.autoindent = value |
|
358 | self.autoindent = value | |
359 |
|
359 | |||
360 | #------------------------------------------------------------------------- |
|
360 | #------------------------------------------------------------------------- | |
361 | # init_* methods called by __init__ |
|
361 | # init_* methods called by __init__ | |
362 | #------------------------------------------------------------------------- |
|
362 | #------------------------------------------------------------------------- | |
363 |
|
363 | |||
364 | def init_ipython_dir(self, ipython_dir): |
|
364 | def init_ipython_dir(self, ipython_dir): | |
365 | if ipython_dir is not None: |
|
365 | if ipython_dir is not None: | |
366 | self.ipython_dir = ipython_dir |
|
366 | self.ipython_dir = ipython_dir | |
367 | self.config.Global.ipython_dir = self.ipython_dir |
|
367 | self.config.Global.ipython_dir = self.ipython_dir | |
368 | return |
|
368 | return | |
369 |
|
369 | |||
370 | if hasattr(self.config.Global, 'ipython_dir'): |
|
370 | if hasattr(self.config.Global, 'ipython_dir'): | |
371 | self.ipython_dir = self.config.Global.ipython_dir |
|
371 | self.ipython_dir = self.config.Global.ipython_dir | |
372 | else: |
|
372 | else: | |
373 | self.ipython_dir = get_ipython_dir() |
|
373 | self.ipython_dir = get_ipython_dir() | |
374 |
|
374 | |||
375 | # All children can just read this |
|
375 | # All children can just read this | |
376 | self.config.Global.ipython_dir = self.ipython_dir |
|
376 | self.config.Global.ipython_dir = self.ipython_dir | |
377 |
|
377 | |||
378 | def init_instance_attrs(self): |
|
378 | def init_instance_attrs(self): | |
379 | self.more = False |
|
379 | self.more = False | |
380 |
|
380 | |||
381 | # command compiler |
|
381 | # command compiler | |
382 | self.compile = CachingCompiler() |
|
382 | self.compile = CachingCompiler() | |
383 |
|
383 | |||
384 | # User input buffers |
|
384 | # User input buffers | |
385 | # NOTE: these variables are slated for full removal, once we are 100% |
|
385 | # NOTE: these variables are slated for full removal, once we are 100% | |
386 | # sure that the new execution logic is solid. We will delte runlines, |
|
386 | # sure that the new execution logic is solid. We will delte runlines, | |
387 | # push_line and these buffers, as all input will be managed by the |
|
387 | # push_line and these buffers, as all input will be managed by the | |
388 | # frontends via an inputsplitter instance. |
|
388 | # frontends via an inputsplitter instance. | |
389 | self.buffer = [] |
|
389 | self.buffer = [] | |
390 | self.buffer_raw = [] |
|
390 | self.buffer_raw = [] | |
391 |
|
391 | |||
392 | # Make an empty namespace, which extension writers can rely on both |
|
392 | # Make an empty namespace, which extension writers can rely on both | |
393 | # existing and NEVER being used by ipython itself. This gives them a |
|
393 | # existing and NEVER being used by ipython itself. This gives them a | |
394 | # convenient location for storing additional information and state |
|
394 | # convenient location for storing additional information and state | |
395 | # their extensions may require, without fear of collisions with other |
|
395 | # their extensions may require, without fear of collisions with other | |
396 | # ipython names that may develop later. |
|
396 | # ipython names that may develop later. | |
397 | self.meta = Struct() |
|
397 | self.meta = Struct() | |
398 |
|
398 | |||
399 | # Object variable to store code object waiting execution. This is |
|
399 | # Object variable to store code object waiting execution. This is | |
400 | # used mainly by the multithreaded shells, but it can come in handy in |
|
400 | # used mainly by the multithreaded shells, but it can come in handy in | |
401 | # other situations. No need to use a Queue here, since it's a single |
|
401 | # other situations. No need to use a Queue here, since it's a single | |
402 | # item which gets cleared once run. |
|
402 | # item which gets cleared once run. | |
403 | self.code_to_run = None |
|
403 | self.code_to_run = None | |
404 |
|
404 | |||
405 | # Temporary files used for various purposes. Deleted at exit. |
|
405 | # Temporary files used for various purposes. Deleted at exit. | |
406 | self.tempfiles = [] |
|
406 | self.tempfiles = [] | |
407 |
|
407 | |||
408 | # Keep track of readline usage (later set by init_readline) |
|
408 | # Keep track of readline usage (later set by init_readline) | |
409 | self.has_readline = False |
|
409 | self.has_readline = False | |
410 |
|
410 | |||
411 | # keep track of where we started running (mainly for crash post-mortem) |
|
411 | # keep track of where we started running (mainly for crash post-mortem) | |
412 | # This is not being used anywhere currently. |
|
412 | # This is not being used anywhere currently. | |
413 | self.starting_dir = os.getcwd() |
|
413 | self.starting_dir = os.getcwd() | |
414 |
|
414 | |||
415 | # Indentation management |
|
415 | # Indentation management | |
416 | self.indent_current_nsp = 0 |
|
416 | self.indent_current_nsp = 0 | |
417 |
|
417 | |||
418 | def init_environment(self): |
|
418 | def init_environment(self): | |
419 | """Any changes we need to make to the user's environment.""" |
|
419 | """Any changes we need to make to the user's environment.""" | |
420 | pass |
|
420 | pass | |
421 |
|
421 | |||
422 | def init_encoding(self): |
|
422 | def init_encoding(self): | |
423 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
423 | # Get system encoding at startup time. Certain terminals (like Emacs | |
424 | # under Win32 have it set to None, and we need to have a known valid |
|
424 | # under Win32 have it set to None, and we need to have a known valid | |
425 | # encoding to use in the raw_input() method |
|
425 | # encoding to use in the raw_input() method | |
426 | try: |
|
426 | try: | |
427 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
427 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
428 | except AttributeError: |
|
428 | except AttributeError: | |
429 | self.stdin_encoding = 'ascii' |
|
429 | self.stdin_encoding = 'ascii' | |
430 |
|
430 | |||
431 | def init_syntax_highlighting(self): |
|
431 | def init_syntax_highlighting(self): | |
432 | # Python source parser/formatter for syntax highlighting |
|
432 | # Python source parser/formatter for syntax highlighting | |
433 | pyformat = PyColorize.Parser().format |
|
433 | pyformat = PyColorize.Parser().format | |
434 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
434 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
435 |
|
435 | |||
436 | def init_pushd_popd_magic(self): |
|
436 | def init_pushd_popd_magic(self): | |
437 | # for pushd/popd management |
|
437 | # for pushd/popd management | |
438 | try: |
|
438 | try: | |
439 | self.home_dir = get_home_dir() |
|
439 | self.home_dir = get_home_dir() | |
440 | except HomeDirError, msg: |
|
440 | except HomeDirError, msg: | |
441 | fatal(msg) |
|
441 | fatal(msg) | |
442 |
|
442 | |||
443 | self.dir_stack = [] |
|
443 | self.dir_stack = [] | |
444 |
|
444 | |||
445 | def init_logger(self): |
|
445 | def init_logger(self): | |
446 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
446 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
447 | logmode='rotate') |
|
447 | logmode='rotate') | |
448 |
|
448 | |||
449 | def init_logstart(self): |
|
449 | def init_logstart(self): | |
450 | """Initialize logging in case it was requested at the command line. |
|
450 | """Initialize logging in case it was requested at the command line. | |
451 | """ |
|
451 | """ | |
452 | if self.logappend: |
|
452 | if self.logappend: | |
453 | self.magic_logstart(self.logappend + ' append') |
|
453 | self.magic_logstart(self.logappend + ' append') | |
454 | elif self.logfile: |
|
454 | elif self.logfile: | |
455 | self.magic_logstart(self.logfile) |
|
455 | self.magic_logstart(self.logfile) | |
456 | elif self.logstart: |
|
456 | elif self.logstart: | |
457 | self.magic_logstart() |
|
457 | self.magic_logstart() | |
458 |
|
458 | |||
459 | def init_builtins(self): |
|
459 | def init_builtins(self): | |
460 | self.builtin_trap = BuiltinTrap(shell=self) |
|
460 | self.builtin_trap = BuiltinTrap(shell=self) | |
461 |
|
461 | |||
462 | def init_inspector(self): |
|
462 | def init_inspector(self): | |
463 | # Object inspector |
|
463 | # Object inspector | |
464 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
464 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
465 | PyColorize.ANSICodeColors, |
|
465 | PyColorize.ANSICodeColors, | |
466 | 'NoColor', |
|
466 | 'NoColor', | |
467 | self.object_info_string_level) |
|
467 | self.object_info_string_level) | |
468 |
|
468 | |||
469 | def init_io(self): |
|
469 | def init_io(self): | |
470 | # This will just use sys.stdout and sys.stderr. If you want to |
|
470 | # This will just use sys.stdout and sys.stderr. If you want to | |
471 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
471 | # override sys.stdout and sys.stderr themselves, you need to do that | |
472 | # *before* instantiating this class, because Term holds onto |
|
472 | # *before* instantiating this class, because Term holds onto | |
473 | # references to the underlying streams. |
|
473 | # references to the underlying streams. | |
474 | if sys.platform == 'win32' and self.has_readline: |
|
474 | if sys.platform == 'win32' and self.has_readline: | |
475 | Term = io.IOTerm(cout=self.readline._outputfile, |
|
475 | Term = io.IOTerm(cout=self.readline._outputfile, | |
476 | cerr=self.readline._outputfile) |
|
476 | cerr=self.readline._outputfile) | |
477 | else: |
|
477 | else: | |
478 | Term = io.IOTerm() |
|
478 | Term = io.IOTerm() | |
479 | io.Term = Term |
|
479 | io.Term = Term | |
480 |
|
480 | |||
481 | def init_prompts(self): |
|
481 | def init_prompts(self): | |
482 | # TODO: This is a pass for now because the prompts are managed inside |
|
482 | # TODO: This is a pass for now because the prompts are managed inside | |
483 | # the DisplayHook. Once there is a separate prompt manager, this |
|
483 | # the DisplayHook. Once there is a separate prompt manager, this | |
484 | # will initialize that object and all prompt related information. |
|
484 | # will initialize that object and all prompt related information. | |
485 | pass |
|
485 | pass | |
486 |
|
486 | |||
487 | def init_display_formatter(self): |
|
487 | def init_display_formatter(self): | |
488 | self.display_formatter = DisplayFormatter(config=self.config) |
|
488 | self.display_formatter = DisplayFormatter(config=self.config) | |
489 |
|
489 | |||
490 | def init_display_pub(self): |
|
490 | def init_display_pub(self): | |
491 | self.display_pub = self.display_pub_class(config=self.config) |
|
491 | self.display_pub = self.display_pub_class(config=self.config) | |
492 |
|
492 | |||
493 | def init_displayhook(self): |
|
493 | def init_displayhook(self): | |
494 | # Initialize displayhook, set in/out prompts and printing system |
|
494 | # Initialize displayhook, set in/out prompts and printing system | |
495 | self.displayhook = self.displayhook_class( |
|
495 | self.displayhook = self.displayhook_class( | |
496 | config=self.config, |
|
496 | config=self.config, | |
497 | shell=self, |
|
497 | shell=self, | |
498 | cache_size=self.cache_size, |
|
498 | cache_size=self.cache_size, | |
499 | input_sep = self.separate_in, |
|
499 | input_sep = self.separate_in, | |
500 | output_sep = self.separate_out, |
|
500 | output_sep = self.separate_out, | |
501 | output_sep2 = self.separate_out2, |
|
501 | output_sep2 = self.separate_out2, | |
502 | ps1 = self.prompt_in1, |
|
502 | ps1 = self.prompt_in1, | |
503 | ps2 = self.prompt_in2, |
|
503 | ps2 = self.prompt_in2, | |
504 | ps_out = self.prompt_out, |
|
504 | ps_out = self.prompt_out, | |
505 | pad_left = self.prompts_pad_left |
|
505 | pad_left = self.prompts_pad_left | |
506 | ) |
|
506 | ) | |
507 | # This is a context manager that installs/revmoes the displayhook at |
|
507 | # This is a context manager that installs/revmoes the displayhook at | |
508 | # the appropriate time. |
|
508 | # the appropriate time. | |
509 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
509 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
510 |
|
510 | |||
511 | def init_reload_doctest(self): |
|
511 | def init_reload_doctest(self): | |
512 | # Do a proper resetting of doctest, including the necessary displayhook |
|
512 | # Do a proper resetting of doctest, including the necessary displayhook | |
513 | # monkeypatching |
|
513 | # monkeypatching | |
514 | try: |
|
514 | try: | |
515 | doctest_reload() |
|
515 | doctest_reload() | |
516 | except ImportError: |
|
516 | except ImportError: | |
517 | warn("doctest module does not exist.") |
|
517 | warn("doctest module does not exist.") | |
518 |
|
518 | |||
519 | #------------------------------------------------------------------------- |
|
519 | #------------------------------------------------------------------------- | |
520 | # Things related to injections into the sys module |
|
520 | # Things related to injections into the sys module | |
521 | #------------------------------------------------------------------------- |
|
521 | #------------------------------------------------------------------------- | |
522 |
|
522 | |||
523 | def save_sys_module_state(self): |
|
523 | def save_sys_module_state(self): | |
524 | """Save the state of hooks in the sys module. |
|
524 | """Save the state of hooks in the sys module. | |
525 |
|
525 | |||
526 | This has to be called after self.user_ns is created. |
|
526 | This has to be called after self.user_ns is created. | |
527 | """ |
|
527 | """ | |
528 | self._orig_sys_module_state = {} |
|
528 | self._orig_sys_module_state = {} | |
529 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
529 | self._orig_sys_module_state['stdin'] = sys.stdin | |
530 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
530 | self._orig_sys_module_state['stdout'] = sys.stdout | |
531 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
531 | self._orig_sys_module_state['stderr'] = sys.stderr | |
532 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
532 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
533 | try: |
|
533 | try: | |
534 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
534 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
535 | except KeyError: |
|
535 | except KeyError: | |
536 | pass |
|
536 | pass | |
537 |
|
537 | |||
538 | def restore_sys_module_state(self): |
|
538 | def restore_sys_module_state(self): | |
539 | """Restore the state of the sys module.""" |
|
539 | """Restore the state of the sys module.""" | |
540 | try: |
|
540 | try: | |
541 | for k, v in self._orig_sys_module_state.iteritems(): |
|
541 | for k, v in self._orig_sys_module_state.iteritems(): | |
542 | setattr(sys, k, v) |
|
542 | setattr(sys, k, v) | |
543 | except AttributeError: |
|
543 | except AttributeError: | |
544 | pass |
|
544 | pass | |
545 | # Reset what what done in self.init_sys_modules |
|
545 | # Reset what what done in self.init_sys_modules | |
546 | try: |
|
546 | try: | |
547 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
547 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name | |
548 | except (AttributeError, KeyError): |
|
548 | except (AttributeError, KeyError): | |
549 | pass |
|
549 | pass | |
550 |
|
550 | |||
551 | #------------------------------------------------------------------------- |
|
551 | #------------------------------------------------------------------------- | |
552 | # Things related to hooks |
|
552 | # Things related to hooks | |
553 | #------------------------------------------------------------------------- |
|
553 | #------------------------------------------------------------------------- | |
554 |
|
554 | |||
555 | def init_hooks(self): |
|
555 | def init_hooks(self): | |
556 | # hooks holds pointers used for user-side customizations |
|
556 | # hooks holds pointers used for user-side customizations | |
557 | self.hooks = Struct() |
|
557 | self.hooks = Struct() | |
558 |
|
558 | |||
559 | self.strdispatchers = {} |
|
559 | self.strdispatchers = {} | |
560 |
|
560 | |||
561 | # Set all default hooks, defined in the IPython.hooks module. |
|
561 | # Set all default hooks, defined in the IPython.hooks module. | |
562 | hooks = IPython.core.hooks |
|
562 | hooks = IPython.core.hooks | |
563 | for hook_name in hooks.__all__: |
|
563 | for hook_name in hooks.__all__: | |
564 | # default hooks have priority 100, i.e. low; user hooks should have |
|
564 | # default hooks have priority 100, i.e. low; user hooks should have | |
565 | # 0-100 priority |
|
565 | # 0-100 priority | |
566 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
566 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
567 |
|
567 | |||
568 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
568 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
569 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
569 | """set_hook(name,hook) -> sets an internal IPython hook. | |
570 |
|
570 | |||
571 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
571 | IPython exposes some of its internal API as user-modifiable hooks. By | |
572 | adding your function to one of these hooks, you can modify IPython's |
|
572 | adding your function to one of these hooks, you can modify IPython's | |
573 | behavior to call at runtime your own routines.""" |
|
573 | behavior to call at runtime your own routines.""" | |
574 |
|
574 | |||
575 | # At some point in the future, this should validate the hook before it |
|
575 | # At some point in the future, this should validate the hook before it | |
576 | # accepts it. Probably at least check that the hook takes the number |
|
576 | # accepts it. Probably at least check that the hook takes the number | |
577 | # of args it's supposed to. |
|
577 | # of args it's supposed to. | |
578 |
|
578 | |||
579 | f = types.MethodType(hook,self) |
|
579 | f = types.MethodType(hook,self) | |
580 |
|
580 | |||
581 | # check if the hook is for strdispatcher first |
|
581 | # check if the hook is for strdispatcher first | |
582 | if str_key is not None: |
|
582 | if str_key is not None: | |
583 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
583 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
584 | sdp.add_s(str_key, f, priority ) |
|
584 | sdp.add_s(str_key, f, priority ) | |
585 | self.strdispatchers[name] = sdp |
|
585 | self.strdispatchers[name] = sdp | |
586 | return |
|
586 | return | |
587 | if re_key is not None: |
|
587 | if re_key is not None: | |
588 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
588 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
589 | sdp.add_re(re.compile(re_key), f, priority ) |
|
589 | sdp.add_re(re.compile(re_key), f, priority ) | |
590 | self.strdispatchers[name] = sdp |
|
590 | self.strdispatchers[name] = sdp | |
591 | return |
|
591 | return | |
592 |
|
592 | |||
593 | dp = getattr(self.hooks, name, None) |
|
593 | dp = getattr(self.hooks, name, None) | |
594 | if name not in IPython.core.hooks.__all__: |
|
594 | if name not in IPython.core.hooks.__all__: | |
595 | print "Warning! Hook '%s' is not one of %s" % \ |
|
595 | print "Warning! Hook '%s' is not one of %s" % \ | |
596 | (name, IPython.core.hooks.__all__ ) |
|
596 | (name, IPython.core.hooks.__all__ ) | |
597 | if not dp: |
|
597 | if not dp: | |
598 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
598 | dp = IPython.core.hooks.CommandChainDispatcher() | |
599 |
|
599 | |||
600 | try: |
|
600 | try: | |
601 | dp.add(f,priority) |
|
601 | dp.add(f,priority) | |
602 | except AttributeError: |
|
602 | except AttributeError: | |
603 | # it was not commandchain, plain old func - replace |
|
603 | # it was not commandchain, plain old func - replace | |
604 | dp = f |
|
604 | dp = f | |
605 |
|
605 | |||
606 | setattr(self.hooks,name, dp) |
|
606 | setattr(self.hooks,name, dp) | |
607 |
|
607 | |||
608 | def register_post_execute(self, func): |
|
608 | def register_post_execute(self, func): | |
609 | """Register a function for calling after code execution. |
|
609 | """Register a function for calling after code execution. | |
610 | """ |
|
610 | """ | |
611 | if not callable(func): |
|
611 | if not callable(func): | |
612 | raise ValueError('argument %s must be callable' % func) |
|
612 | raise ValueError('argument %s must be callable' % func) | |
613 | self._post_execute.add(func) |
|
613 | self._post_execute.add(func) | |
614 |
|
614 | |||
615 | #------------------------------------------------------------------------- |
|
615 | #------------------------------------------------------------------------- | |
616 | # Things related to the "main" module |
|
616 | # Things related to the "main" module | |
617 | #------------------------------------------------------------------------- |
|
617 | #------------------------------------------------------------------------- | |
618 |
|
618 | |||
619 | def new_main_mod(self,ns=None): |
|
619 | def new_main_mod(self,ns=None): | |
620 | """Return a new 'main' module object for user code execution. |
|
620 | """Return a new 'main' module object for user code execution. | |
621 | """ |
|
621 | """ | |
622 | main_mod = self._user_main_module |
|
622 | main_mod = self._user_main_module | |
623 | init_fakemod_dict(main_mod,ns) |
|
623 | init_fakemod_dict(main_mod,ns) | |
624 | return main_mod |
|
624 | return main_mod | |
625 |
|
625 | |||
626 | def cache_main_mod(self,ns,fname): |
|
626 | def cache_main_mod(self,ns,fname): | |
627 | """Cache a main module's namespace. |
|
627 | """Cache a main module's namespace. | |
628 |
|
628 | |||
629 | When scripts are executed via %run, we must keep a reference to the |
|
629 | When scripts are executed via %run, we must keep a reference to the | |
630 | namespace of their __main__ module (a FakeModule instance) around so |
|
630 | namespace of their __main__ module (a FakeModule instance) around so | |
631 | that Python doesn't clear it, rendering objects defined therein |
|
631 | that Python doesn't clear it, rendering objects defined therein | |
632 | useless. |
|
632 | useless. | |
633 |
|
633 | |||
634 | This method keeps said reference in a private dict, keyed by the |
|
634 | This method keeps said reference in a private dict, keyed by the | |
635 | absolute path of the module object (which corresponds to the script |
|
635 | absolute path of the module object (which corresponds to the script | |
636 | path). This way, for multiple executions of the same script we only |
|
636 | path). This way, for multiple executions of the same script we only | |
637 | keep one copy of the namespace (the last one), thus preventing memory |
|
637 | keep one copy of the namespace (the last one), thus preventing memory | |
638 | leaks from old references while allowing the objects from the last |
|
638 | leaks from old references while allowing the objects from the last | |
639 | execution to be accessible. |
|
639 | execution to be accessible. | |
640 |
|
640 | |||
641 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
641 | Note: we can not allow the actual FakeModule instances to be deleted, | |
642 | because of how Python tears down modules (it hard-sets all their |
|
642 | because of how Python tears down modules (it hard-sets all their | |
643 | references to None without regard for reference counts). This method |
|
643 | references to None without regard for reference counts). This method | |
644 | must therefore make a *copy* of the given namespace, to allow the |
|
644 | must therefore make a *copy* of the given namespace, to allow the | |
645 | original module's __dict__ to be cleared and reused. |
|
645 | original module's __dict__ to be cleared and reused. | |
646 |
|
646 | |||
647 |
|
647 | |||
648 | Parameters |
|
648 | Parameters | |
649 | ---------- |
|
649 | ---------- | |
650 | ns : a namespace (a dict, typically) |
|
650 | ns : a namespace (a dict, typically) | |
651 |
|
651 | |||
652 | fname : str |
|
652 | fname : str | |
653 | Filename associated with the namespace. |
|
653 | Filename associated with the namespace. | |
654 |
|
654 | |||
655 | Examples |
|
655 | Examples | |
656 | -------- |
|
656 | -------- | |
657 |
|
657 | |||
658 | In [10]: import IPython |
|
658 | In [10]: import IPython | |
659 |
|
659 | |||
660 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
660 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
661 |
|
661 | |||
662 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
662 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
663 | Out[12]: True |
|
663 | Out[12]: True | |
664 | """ |
|
664 | """ | |
665 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
665 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
666 |
|
666 | |||
667 | def clear_main_mod_cache(self): |
|
667 | def clear_main_mod_cache(self): | |
668 | """Clear the cache of main modules. |
|
668 | """Clear the cache of main modules. | |
669 |
|
669 | |||
670 | Mainly for use by utilities like %reset. |
|
670 | Mainly for use by utilities like %reset. | |
671 |
|
671 | |||
672 | Examples |
|
672 | Examples | |
673 | -------- |
|
673 | -------- | |
674 |
|
674 | |||
675 | In [15]: import IPython |
|
675 | In [15]: import IPython | |
676 |
|
676 | |||
677 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
677 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
678 |
|
678 | |||
679 | In [17]: len(_ip._main_ns_cache) > 0 |
|
679 | In [17]: len(_ip._main_ns_cache) > 0 | |
680 | Out[17]: True |
|
680 | Out[17]: True | |
681 |
|
681 | |||
682 | In [18]: _ip.clear_main_mod_cache() |
|
682 | In [18]: _ip.clear_main_mod_cache() | |
683 |
|
683 | |||
684 | In [19]: len(_ip._main_ns_cache) == 0 |
|
684 | In [19]: len(_ip._main_ns_cache) == 0 | |
685 | Out[19]: True |
|
685 | Out[19]: True | |
686 | """ |
|
686 | """ | |
687 | self._main_ns_cache.clear() |
|
687 | self._main_ns_cache.clear() | |
688 |
|
688 | |||
689 | #------------------------------------------------------------------------- |
|
689 | #------------------------------------------------------------------------- | |
690 | # Things related to debugging |
|
690 | # Things related to debugging | |
691 | #------------------------------------------------------------------------- |
|
691 | #------------------------------------------------------------------------- | |
692 |
|
692 | |||
693 | def init_pdb(self): |
|
693 | def init_pdb(self): | |
694 | # Set calling of pdb on exceptions |
|
694 | # Set calling of pdb on exceptions | |
695 | # self.call_pdb is a property |
|
695 | # self.call_pdb is a property | |
696 | self.call_pdb = self.pdb |
|
696 | self.call_pdb = self.pdb | |
697 |
|
697 | |||
698 | def _get_call_pdb(self): |
|
698 | def _get_call_pdb(self): | |
699 | return self._call_pdb |
|
699 | return self._call_pdb | |
700 |
|
700 | |||
701 | def _set_call_pdb(self,val): |
|
701 | def _set_call_pdb(self,val): | |
702 |
|
702 | |||
703 | if val not in (0,1,False,True): |
|
703 | if val not in (0,1,False,True): | |
704 | raise ValueError,'new call_pdb value must be boolean' |
|
704 | raise ValueError,'new call_pdb value must be boolean' | |
705 |
|
705 | |||
706 | # store value in instance |
|
706 | # store value in instance | |
707 | self._call_pdb = val |
|
707 | self._call_pdb = val | |
708 |
|
708 | |||
709 | # notify the actual exception handlers |
|
709 | # notify the actual exception handlers | |
710 | self.InteractiveTB.call_pdb = val |
|
710 | self.InteractiveTB.call_pdb = val | |
711 |
|
711 | |||
712 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
712 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
713 | 'Control auto-activation of pdb at exceptions') |
|
713 | 'Control auto-activation of pdb at exceptions') | |
714 |
|
714 | |||
715 | def debugger(self,force=False): |
|
715 | def debugger(self,force=False): | |
716 | """Call the pydb/pdb debugger. |
|
716 | """Call the pydb/pdb debugger. | |
717 |
|
717 | |||
718 | Keywords: |
|
718 | Keywords: | |
719 |
|
719 | |||
720 | - force(False): by default, this routine checks the instance call_pdb |
|
720 | - force(False): by default, this routine checks the instance call_pdb | |
721 | flag and does not actually invoke the debugger if the flag is false. |
|
721 | flag and does not actually invoke the debugger if the flag is false. | |
722 | The 'force' option forces the debugger to activate even if the flag |
|
722 | The 'force' option forces the debugger to activate even if the flag | |
723 | is false. |
|
723 | is false. | |
724 | """ |
|
724 | """ | |
725 |
|
725 | |||
726 | if not (force or self.call_pdb): |
|
726 | if not (force or self.call_pdb): | |
727 | return |
|
727 | return | |
728 |
|
728 | |||
729 | if not hasattr(sys,'last_traceback'): |
|
729 | if not hasattr(sys,'last_traceback'): | |
730 | error('No traceback has been produced, nothing to debug.') |
|
730 | error('No traceback has been produced, nothing to debug.') | |
731 | return |
|
731 | return | |
732 |
|
732 | |||
733 | # use pydb if available |
|
733 | # use pydb if available | |
734 | if debugger.has_pydb: |
|
734 | if debugger.has_pydb: | |
735 | from pydb import pm |
|
735 | from pydb import pm | |
736 | else: |
|
736 | else: | |
737 | # fallback to our internal debugger |
|
737 | # fallback to our internal debugger | |
738 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
738 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
739 | self.history_saving_wrapper(pm)() |
|
739 | self.history_saving_wrapper(pm)() | |
740 |
|
740 | |||
741 | #------------------------------------------------------------------------- |
|
741 | #------------------------------------------------------------------------- | |
742 | # Things related to IPython's various namespaces |
|
742 | # Things related to IPython's various namespaces | |
743 | #------------------------------------------------------------------------- |
|
743 | #------------------------------------------------------------------------- | |
744 |
|
744 | |||
745 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
745 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
746 | # Create the namespace where the user will operate. user_ns is |
|
746 | # Create the namespace where the user will operate. user_ns is | |
747 | # normally the only one used, and it is passed to the exec calls as |
|
747 | # normally the only one used, and it is passed to the exec calls as | |
748 | # the locals argument. But we do carry a user_global_ns namespace |
|
748 | # the locals argument. But we do carry a user_global_ns namespace | |
749 | # given as the exec 'globals' argument, This is useful in embedding |
|
749 | # given as the exec 'globals' argument, This is useful in embedding | |
750 | # situations where the ipython shell opens in a context where the |
|
750 | # situations where the ipython shell opens in a context where the | |
751 | # distinction between locals and globals is meaningful. For |
|
751 | # distinction between locals and globals is meaningful. For | |
752 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
752 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
753 |
|
753 | |||
754 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
754 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
755 | # level as a dict instead of a module. This is a manual fix, but I |
|
755 | # level as a dict instead of a module. This is a manual fix, but I | |
756 | # should really track down where the problem is coming from. Alex |
|
756 | # should really track down where the problem is coming from. Alex | |
757 | # Schmolck reported this problem first. |
|
757 | # Schmolck reported this problem first. | |
758 |
|
758 | |||
759 | # A useful post by Alex Martelli on this topic: |
|
759 | # A useful post by Alex Martelli on this topic: | |
760 | # Re: inconsistent value from __builtins__ |
|
760 | # Re: inconsistent value from __builtins__ | |
761 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
761 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
762 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
762 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
763 | # Gruppen: comp.lang.python |
|
763 | # Gruppen: comp.lang.python | |
764 |
|
764 | |||
765 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
765 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
766 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
766 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
767 | # > <type 'dict'> |
|
767 | # > <type 'dict'> | |
768 | # > >>> print type(__builtins__) |
|
768 | # > >>> print type(__builtins__) | |
769 | # > <type 'module'> |
|
769 | # > <type 'module'> | |
770 | # > Is this difference in return value intentional? |
|
770 | # > Is this difference in return value intentional? | |
771 |
|
771 | |||
772 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
772 | # Well, it's documented that '__builtins__' can be either a dictionary | |
773 | # or a module, and it's been that way for a long time. Whether it's |
|
773 | # or a module, and it's been that way for a long time. Whether it's | |
774 | # intentional (or sensible), I don't know. In any case, the idea is |
|
774 | # intentional (or sensible), I don't know. In any case, the idea is | |
775 | # that if you need to access the built-in namespace directly, you |
|
775 | # that if you need to access the built-in namespace directly, you | |
776 | # should start with "import __builtin__" (note, no 's') which will |
|
776 | # should start with "import __builtin__" (note, no 's') which will | |
777 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
777 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
778 |
|
778 | |||
779 | # These routines return properly built dicts as needed by the rest of |
|
779 | # These routines return properly built dicts as needed by the rest of | |
780 | # the code, and can also be used by extension writers to generate |
|
780 | # the code, and can also be used by extension writers to generate | |
781 | # properly initialized namespaces. |
|
781 | # properly initialized namespaces. | |
782 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
782 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, | |
783 | user_global_ns) |
|
783 | user_global_ns) | |
784 |
|
784 | |||
785 | # Assign namespaces |
|
785 | # Assign namespaces | |
786 | # This is the namespace where all normal user variables live |
|
786 | # This is the namespace where all normal user variables live | |
787 | self.user_ns = user_ns |
|
787 | self.user_ns = user_ns | |
788 | self.user_global_ns = user_global_ns |
|
788 | self.user_global_ns = user_global_ns | |
789 |
|
789 | |||
790 | # An auxiliary namespace that checks what parts of the user_ns were |
|
790 | # An auxiliary namespace that checks what parts of the user_ns were | |
791 | # loaded at startup, so we can list later only variables defined in |
|
791 | # loaded at startup, so we can list later only variables defined in | |
792 | # actual interactive use. Since it is always a subset of user_ns, it |
|
792 | # actual interactive use. Since it is always a subset of user_ns, it | |
793 | # doesn't need to be separately tracked in the ns_table. |
|
793 | # doesn't need to be separately tracked in the ns_table. | |
794 | self.user_ns_hidden = {} |
|
794 | self.user_ns_hidden = {} | |
795 |
|
795 | |||
796 | # A namespace to keep track of internal data structures to prevent |
|
796 | # A namespace to keep track of internal data structures to prevent | |
797 | # them from cluttering user-visible stuff. Will be updated later |
|
797 | # them from cluttering user-visible stuff. Will be updated later | |
798 | self.internal_ns = {} |
|
798 | self.internal_ns = {} | |
799 |
|
799 | |||
800 | # Now that FakeModule produces a real module, we've run into a nasty |
|
800 | # Now that FakeModule produces a real module, we've run into a nasty | |
801 | # problem: after script execution (via %run), the module where the user |
|
801 | # problem: after script execution (via %run), the module where the user | |
802 | # code ran is deleted. Now that this object is a true module (needed |
|
802 | # code ran is deleted. Now that this object is a true module (needed | |
803 | # so docetst and other tools work correctly), the Python module |
|
803 | # so docetst and other tools work correctly), the Python module | |
804 | # teardown mechanism runs over it, and sets to None every variable |
|
804 | # teardown mechanism runs over it, and sets to None every variable | |
805 | # present in that module. Top-level references to objects from the |
|
805 | # present in that module. Top-level references to objects from the | |
806 | # script survive, because the user_ns is updated with them. However, |
|
806 | # script survive, because the user_ns is updated with them. However, | |
807 | # calling functions defined in the script that use other things from |
|
807 | # calling functions defined in the script that use other things from | |
808 | # the script will fail, because the function's closure had references |
|
808 | # the script will fail, because the function's closure had references | |
809 | # to the original objects, which are now all None. So we must protect |
|
809 | # to the original objects, which are now all None. So we must protect | |
810 | # these modules from deletion by keeping a cache. |
|
810 | # these modules from deletion by keeping a cache. | |
811 | # |
|
811 | # | |
812 | # To avoid keeping stale modules around (we only need the one from the |
|
812 | # To avoid keeping stale modules around (we only need the one from the | |
813 | # last run), we use a dict keyed with the full path to the script, so |
|
813 | # last run), we use a dict keyed with the full path to the script, so | |
814 | # only the last version of the module is held in the cache. Note, |
|
814 | # only the last version of the module is held in the cache. Note, | |
815 | # however, that we must cache the module *namespace contents* (their |
|
815 | # however, that we must cache the module *namespace contents* (their | |
816 | # __dict__). Because if we try to cache the actual modules, old ones |
|
816 | # __dict__). Because if we try to cache the actual modules, old ones | |
817 | # (uncached) could be destroyed while still holding references (such as |
|
817 | # (uncached) could be destroyed while still holding references (such as | |
818 | # those held by GUI objects that tend to be long-lived)> |
|
818 | # those held by GUI objects that tend to be long-lived)> | |
819 | # |
|
819 | # | |
820 | # The %reset command will flush this cache. See the cache_main_mod() |
|
820 | # The %reset command will flush this cache. See the cache_main_mod() | |
821 | # and clear_main_mod_cache() methods for details on use. |
|
821 | # and clear_main_mod_cache() methods for details on use. | |
822 |
|
822 | |||
823 | # This is the cache used for 'main' namespaces |
|
823 | # This is the cache used for 'main' namespaces | |
824 | self._main_ns_cache = {} |
|
824 | self._main_ns_cache = {} | |
825 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
825 | # And this is the single instance of FakeModule whose __dict__ we keep | |
826 | # copying and clearing for reuse on each %run |
|
826 | # copying and clearing for reuse on each %run | |
827 | self._user_main_module = FakeModule() |
|
827 | self._user_main_module = FakeModule() | |
828 |
|
828 | |||
829 | # A table holding all the namespaces IPython deals with, so that |
|
829 | # A table holding all the namespaces IPython deals with, so that | |
830 | # introspection facilities can search easily. |
|
830 | # introspection facilities can search easily. | |
831 | self.ns_table = {'user':user_ns, |
|
831 | self.ns_table = {'user':user_ns, | |
832 | 'user_global':user_global_ns, |
|
832 | 'user_global':user_global_ns, | |
833 | 'internal':self.internal_ns, |
|
833 | 'internal':self.internal_ns, | |
834 | 'builtin':__builtin__.__dict__ |
|
834 | 'builtin':__builtin__.__dict__ | |
835 | } |
|
835 | } | |
836 |
|
836 | |||
837 | # Similarly, track all namespaces where references can be held and that |
|
837 | # Similarly, track all namespaces where references can be held and that | |
838 | # we can safely clear (so it can NOT include builtin). This one can be |
|
838 | # we can safely clear (so it can NOT include builtin). This one can be | |
839 | # a simple list. Note that the main execution namespaces, user_ns and |
|
839 | # a simple list. Note that the main execution namespaces, user_ns and | |
840 | # user_global_ns, can NOT be listed here, as clearing them blindly |
|
840 | # user_global_ns, can NOT be listed here, as clearing them blindly | |
841 | # causes errors in object __del__ methods. Instead, the reset() method |
|
841 | # causes errors in object __del__ methods. Instead, the reset() method | |
842 | # clears them manually and carefully. |
|
842 | # clears them manually and carefully. | |
843 | self.ns_refs_table = [ self.user_ns_hidden, |
|
843 | self.ns_refs_table = [ self.user_ns_hidden, | |
844 | self.internal_ns, self._main_ns_cache ] |
|
844 | self.internal_ns, self._main_ns_cache ] | |
845 |
|
845 | |||
846 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
846 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
847 | """Return a valid local and global user interactive namespaces. |
|
847 | """Return a valid local and global user interactive namespaces. | |
848 |
|
848 | |||
849 | This builds a dict with the minimal information needed to operate as a |
|
849 | This builds a dict with the minimal information needed to operate as a | |
850 | valid IPython user namespace, which you can pass to the various |
|
850 | valid IPython user namespace, which you can pass to the various | |
851 | embedding classes in ipython. The default implementation returns the |
|
851 | embedding classes in ipython. The default implementation returns the | |
852 | same dict for both the locals and the globals to allow functions to |
|
852 | same dict for both the locals and the globals to allow functions to | |
853 | refer to variables in the namespace. Customized implementations can |
|
853 | refer to variables in the namespace. Customized implementations can | |
854 | return different dicts. The locals dictionary can actually be anything |
|
854 | return different dicts. The locals dictionary can actually be anything | |
855 | following the basic mapping protocol of a dict, but the globals dict |
|
855 | following the basic mapping protocol of a dict, but the globals dict | |
856 | must be a true dict, not even a subclass. It is recommended that any |
|
856 | must be a true dict, not even a subclass. It is recommended that any | |
857 | custom object for the locals namespace synchronize with the globals |
|
857 | custom object for the locals namespace synchronize with the globals | |
858 | dict somehow. |
|
858 | dict somehow. | |
859 |
|
859 | |||
860 | Raises TypeError if the provided globals namespace is not a true dict. |
|
860 | Raises TypeError if the provided globals namespace is not a true dict. | |
861 |
|
861 | |||
862 | Parameters |
|
862 | Parameters | |
863 | ---------- |
|
863 | ---------- | |
864 | user_ns : dict-like, optional |
|
864 | user_ns : dict-like, optional | |
865 | The current user namespace. The items in this namespace should |
|
865 | The current user namespace. The items in this namespace should | |
866 | be included in the output. If None, an appropriate blank |
|
866 | be included in the output. If None, an appropriate blank | |
867 | namespace should be created. |
|
867 | namespace should be created. | |
868 | user_global_ns : dict, optional |
|
868 | user_global_ns : dict, optional | |
869 | The current user global namespace. The items in this namespace |
|
869 | The current user global namespace. The items in this namespace | |
870 | should be included in the output. If None, an appropriate |
|
870 | should be included in the output. If None, an appropriate | |
871 | blank namespace should be created. |
|
871 | blank namespace should be created. | |
872 |
|
872 | |||
873 | Returns |
|
873 | Returns | |
874 | ------- |
|
874 | ------- | |
875 | A pair of dictionary-like object to be used as the local namespace |
|
875 | A pair of dictionary-like object to be used as the local namespace | |
876 | of the interpreter and a dict to be used as the global namespace. |
|
876 | of the interpreter and a dict to be used as the global namespace. | |
877 | """ |
|
877 | """ | |
878 |
|
878 | |||
879 |
|
879 | |||
880 | # We must ensure that __builtin__ (without the final 's') is always |
|
880 | # We must ensure that __builtin__ (without the final 's') is always | |
881 | # available and pointing to the __builtin__ *module*. For more details: |
|
881 | # available and pointing to the __builtin__ *module*. For more details: | |
882 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
882 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
883 |
|
883 | |||
884 | if user_ns is None: |
|
884 | if user_ns is None: | |
885 | # Set __name__ to __main__ to better match the behavior of the |
|
885 | # Set __name__ to __main__ to better match the behavior of the | |
886 | # normal interpreter. |
|
886 | # normal interpreter. | |
887 | user_ns = {'__name__' :'__main__', |
|
887 | user_ns = {'__name__' :'__main__', | |
888 | '__builtin__' : __builtin__, |
|
888 | '__builtin__' : __builtin__, | |
889 | '__builtins__' : __builtin__, |
|
889 | '__builtins__' : __builtin__, | |
890 | } |
|
890 | } | |
891 | else: |
|
891 | else: | |
892 | user_ns.setdefault('__name__','__main__') |
|
892 | user_ns.setdefault('__name__','__main__') | |
893 | user_ns.setdefault('__builtin__',__builtin__) |
|
893 | user_ns.setdefault('__builtin__',__builtin__) | |
894 | user_ns.setdefault('__builtins__',__builtin__) |
|
894 | user_ns.setdefault('__builtins__',__builtin__) | |
895 |
|
895 | |||
896 | if user_global_ns is None: |
|
896 | if user_global_ns is None: | |
897 | user_global_ns = user_ns |
|
897 | user_global_ns = user_ns | |
898 | if type(user_global_ns) is not dict: |
|
898 | if type(user_global_ns) is not dict: | |
899 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
899 | raise TypeError("user_global_ns must be a true dict; got %r" | |
900 | % type(user_global_ns)) |
|
900 | % type(user_global_ns)) | |
901 |
|
901 | |||
902 | return user_ns, user_global_ns |
|
902 | return user_ns, user_global_ns | |
903 |
|
903 | |||
904 | def init_sys_modules(self): |
|
904 | def init_sys_modules(self): | |
905 | # We need to insert into sys.modules something that looks like a |
|
905 | # We need to insert into sys.modules something that looks like a | |
906 | # module but which accesses the IPython namespace, for shelve and |
|
906 | # module but which accesses the IPython namespace, for shelve and | |
907 | # pickle to work interactively. Normally they rely on getting |
|
907 | # pickle to work interactively. Normally they rely on getting | |
908 | # everything out of __main__, but for embedding purposes each IPython |
|
908 | # everything out of __main__, but for embedding purposes each IPython | |
909 | # instance has its own private namespace, so we can't go shoving |
|
909 | # instance has its own private namespace, so we can't go shoving | |
910 | # everything into __main__. |
|
910 | # everything into __main__. | |
911 |
|
911 | |||
912 | # note, however, that we should only do this for non-embedded |
|
912 | # note, however, that we should only do this for non-embedded | |
913 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
913 | # ipythons, which really mimic the __main__.__dict__ with their own | |
914 | # namespace. Embedded instances, on the other hand, should not do |
|
914 | # namespace. Embedded instances, on the other hand, should not do | |
915 | # this because they need to manage the user local/global namespaces |
|
915 | # this because they need to manage the user local/global namespaces | |
916 | # only, but they live within a 'normal' __main__ (meaning, they |
|
916 | # only, but they live within a 'normal' __main__ (meaning, they | |
917 | # shouldn't overtake the execution environment of the script they're |
|
917 | # shouldn't overtake the execution environment of the script they're | |
918 | # embedded in). |
|
918 | # embedded in). | |
919 |
|
919 | |||
920 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
920 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
921 |
|
921 | |||
922 | try: |
|
922 | try: | |
923 | main_name = self.user_ns['__name__'] |
|
923 | main_name = self.user_ns['__name__'] | |
924 | except KeyError: |
|
924 | except KeyError: | |
925 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
925 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
926 | else: |
|
926 | else: | |
927 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
927 | sys.modules[main_name] = FakeModule(self.user_ns) | |
928 |
|
928 | |||
929 | def init_user_ns(self): |
|
929 | def init_user_ns(self): | |
930 | """Initialize all user-visible namespaces to their minimum defaults. |
|
930 | """Initialize all user-visible namespaces to their minimum defaults. | |
931 |
|
931 | |||
932 | Certain history lists are also initialized here, as they effectively |
|
932 | Certain history lists are also initialized here, as they effectively | |
933 | act as user namespaces. |
|
933 | act as user namespaces. | |
934 |
|
934 | |||
935 | Notes |
|
935 | Notes | |
936 | ----- |
|
936 | ----- | |
937 | All data structures here are only filled in, they are NOT reset by this |
|
937 | All data structures here are only filled in, they are NOT reset by this | |
938 | method. If they were not empty before, data will simply be added to |
|
938 | method. If they were not empty before, data will simply be added to | |
939 | therm. |
|
939 | therm. | |
940 | """ |
|
940 | """ | |
941 | # This function works in two parts: first we put a few things in |
|
941 | # This function works in two parts: first we put a few things in | |
942 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
942 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
943 | # initial variables aren't shown by %who. After the sync, we add the |
|
943 | # initial variables aren't shown by %who. After the sync, we add the | |
944 | # rest of what we *do* want the user to see with %who even on a new |
|
944 | # rest of what we *do* want the user to see with %who even on a new | |
945 | # session (probably nothing, so theye really only see their own stuff) |
|
945 | # session (probably nothing, so theye really only see their own stuff) | |
946 |
|
946 | |||
947 | # The user dict must *always* have a __builtin__ reference to the |
|
947 | # The user dict must *always* have a __builtin__ reference to the | |
948 | # Python standard __builtin__ namespace, which must be imported. |
|
948 | # Python standard __builtin__ namespace, which must be imported. | |
949 | # This is so that certain operations in prompt evaluation can be |
|
949 | # This is so that certain operations in prompt evaluation can be | |
950 | # reliably executed with builtins. Note that we can NOT use |
|
950 | # reliably executed with builtins. Note that we can NOT use | |
951 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
951 | # __builtins__ (note the 's'), because that can either be a dict or a | |
952 | # module, and can even mutate at runtime, depending on the context |
|
952 | # module, and can even mutate at runtime, depending on the context | |
953 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
953 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
954 | # always a module object, though it must be explicitly imported. |
|
954 | # always a module object, though it must be explicitly imported. | |
955 |
|
955 | |||
956 | # For more details: |
|
956 | # For more details: | |
957 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
957 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
958 | ns = dict(__builtin__ = __builtin__) |
|
958 | ns = dict(__builtin__ = __builtin__) | |
959 |
|
959 | |||
960 | # Put 'help' in the user namespace |
|
960 | # Put 'help' in the user namespace | |
961 | try: |
|
961 | try: | |
962 | from site import _Helper |
|
962 | from site import _Helper | |
963 | ns['help'] = _Helper() |
|
963 | ns['help'] = _Helper() | |
964 | except ImportError: |
|
964 | except ImportError: | |
965 | warn('help() not available - check site.py') |
|
965 | warn('help() not available - check site.py') | |
966 |
|
966 | |||
967 | # make global variables for user access to the histories |
|
967 | # make global variables for user access to the histories | |
968 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
968 | ns['_ih'] = self.history_manager.input_hist_parsed | |
969 | ns['_oh'] = self.history_manager.output_hist |
|
969 | ns['_oh'] = self.history_manager.output_hist | |
970 | ns['_dh'] = self.history_manager.dir_hist |
|
970 | ns['_dh'] = self.history_manager.dir_hist | |
971 |
|
971 | |||
972 | ns['_sh'] = shadowns |
|
972 | ns['_sh'] = shadowns | |
973 |
|
973 | |||
974 | # user aliases to input and output histories. These shouldn't show up |
|
974 | # user aliases to input and output histories. These shouldn't show up | |
975 | # in %who, as they can have very large reprs. |
|
975 | # in %who, as they can have very large reprs. | |
976 | ns['In'] = self.history_manager.input_hist_parsed |
|
976 | ns['In'] = self.history_manager.input_hist_parsed | |
977 | ns['Out'] = self.history_manager.output_hist |
|
977 | ns['Out'] = self.history_manager.output_hist | |
978 |
|
978 | |||
979 | # Store myself as the public api!!! |
|
979 | # Store myself as the public api!!! | |
980 | ns['get_ipython'] = self.get_ipython |
|
980 | ns['get_ipython'] = self.get_ipython | |
981 |
|
981 | |||
982 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
982 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
983 | # by %who |
|
983 | # by %who | |
984 | self.user_ns_hidden.update(ns) |
|
984 | self.user_ns_hidden.update(ns) | |
985 |
|
985 | |||
986 | # Anything put into ns now would show up in %who. Think twice before |
|
986 | # Anything put into ns now would show up in %who. Think twice before | |
987 | # putting anything here, as we really want %who to show the user their |
|
987 | # putting anything here, as we really want %who to show the user their | |
988 | # stuff, not our variables. |
|
988 | # stuff, not our variables. | |
989 |
|
989 | |||
990 | # Finally, update the real user's namespace |
|
990 | # Finally, update the real user's namespace | |
991 | self.user_ns.update(ns) |
|
991 | self.user_ns.update(ns) | |
992 |
|
992 | |||
993 | def reset(self): |
|
993 | def reset(self, new_session=True): | |
994 | """Clear all internal namespaces. |
|
994 | """Clear all internal namespaces. | |
995 |
|
995 | |||
996 | Note that this is much more aggressive than %reset, since it clears |
|
996 | Note that this is much more aggressive than %reset, since it clears | |
997 | fully all namespaces, as well as all input/output lists. |
|
997 | fully all namespaces, as well as all input/output lists. | |
|
998 | ||||
|
999 | If new_session is True, a new history session will be opened. | |||
998 | """ |
|
1000 | """ | |
999 | # Clear histories |
|
1001 | # Clear histories | |
1000 | self.history_manager.reset() |
|
1002 | self.history_manager.reset(new_session) | |
1001 |
|
1003 | |||
1002 | # Reset counter used to index all histories |
|
1004 | # Reset counter used to index all histories | |
1003 | self.execution_count = 0 |
|
1005 | self.execution_count = 0 | |
1004 |
|
1006 | |||
1005 | # Restore the user namespaces to minimal usability |
|
1007 | # Restore the user namespaces to minimal usability | |
1006 | for ns in self.ns_refs_table: |
|
1008 | for ns in self.ns_refs_table: | |
1007 | ns.clear() |
|
1009 | ns.clear() | |
1008 |
|
1010 | |||
1009 | # The main execution namespaces must be cleared very carefully, |
|
1011 | # The main execution namespaces must be cleared very carefully, | |
1010 | # skipping the deletion of the builtin-related keys, because doing so |
|
1012 | # skipping the deletion of the builtin-related keys, because doing so | |
1011 | # would cause errors in many object's __del__ methods. |
|
1013 | # would cause errors in many object's __del__ methods. | |
1012 | for ns in [self.user_ns, self.user_global_ns]: |
|
1014 | for ns in [self.user_ns, self.user_global_ns]: | |
1013 | drop_keys = set(ns.keys()) |
|
1015 | drop_keys = set(ns.keys()) | |
1014 | drop_keys.discard('__builtin__') |
|
1016 | drop_keys.discard('__builtin__') | |
1015 | drop_keys.discard('__builtins__') |
|
1017 | drop_keys.discard('__builtins__') | |
1016 | for k in drop_keys: |
|
1018 | for k in drop_keys: | |
1017 | del ns[k] |
|
1019 | del ns[k] | |
1018 |
|
1020 | |||
1019 | # Restore the user namespaces to minimal usability |
|
1021 | # Restore the user namespaces to minimal usability | |
1020 | self.init_user_ns() |
|
1022 | self.init_user_ns() | |
1021 |
|
1023 | |||
1022 | # Restore the default and user aliases |
|
1024 | # Restore the default and user aliases | |
1023 | self.alias_manager.clear_aliases() |
|
1025 | self.alias_manager.clear_aliases() | |
1024 | self.alias_manager.init_aliases() |
|
1026 | self.alias_manager.init_aliases() | |
1025 |
|
1027 | |||
1026 | def reset_selective(self, regex=None): |
|
1028 | def reset_selective(self, regex=None): | |
1027 | """Clear selective variables from internal namespaces based on a |
|
1029 | """Clear selective variables from internal namespaces based on a | |
1028 | specified regular expression. |
|
1030 | specified regular expression. | |
1029 |
|
1031 | |||
1030 | Parameters |
|
1032 | Parameters | |
1031 | ---------- |
|
1033 | ---------- | |
1032 | regex : string or compiled pattern, optional |
|
1034 | regex : string or compiled pattern, optional | |
1033 | A regular expression pattern that will be used in searching |
|
1035 | A regular expression pattern that will be used in searching | |
1034 | variable names in the users namespaces. |
|
1036 | variable names in the users namespaces. | |
1035 | """ |
|
1037 | """ | |
1036 | if regex is not None: |
|
1038 | if regex is not None: | |
1037 | try: |
|
1039 | try: | |
1038 | m = re.compile(regex) |
|
1040 | m = re.compile(regex) | |
1039 | except TypeError: |
|
1041 | except TypeError: | |
1040 | raise TypeError('regex must be a string or compiled pattern') |
|
1042 | raise TypeError('regex must be a string or compiled pattern') | |
1041 | # Search for keys in each namespace that match the given regex |
|
1043 | # Search for keys in each namespace that match the given regex | |
1042 | # If a match is found, delete the key/value pair. |
|
1044 | # If a match is found, delete the key/value pair. | |
1043 | for ns in self.ns_refs_table: |
|
1045 | for ns in self.ns_refs_table: | |
1044 | for var in ns: |
|
1046 | for var in ns: | |
1045 | if m.search(var): |
|
1047 | if m.search(var): | |
1046 | del ns[var] |
|
1048 | del ns[var] | |
1047 |
|
1049 | |||
1048 | def push(self, variables, interactive=True): |
|
1050 | def push(self, variables, interactive=True): | |
1049 | """Inject a group of variables into the IPython user namespace. |
|
1051 | """Inject a group of variables into the IPython user namespace. | |
1050 |
|
1052 | |||
1051 | Parameters |
|
1053 | Parameters | |
1052 | ---------- |
|
1054 | ---------- | |
1053 | variables : dict, str or list/tuple of str |
|
1055 | variables : dict, str or list/tuple of str | |
1054 | The variables to inject into the user's namespace. If a dict, a |
|
1056 | The variables to inject into the user's namespace. If a dict, a | |
1055 | simple update is done. If a str, the string is assumed to have |
|
1057 | simple update is done. If a str, the string is assumed to have | |
1056 | variable names separated by spaces. A list/tuple of str can also |
|
1058 | variable names separated by spaces. A list/tuple of str can also | |
1057 | be used to give the variable names. If just the variable names are |
|
1059 | be used to give the variable names. If just the variable names are | |
1058 | give (list/tuple/str) then the variable values looked up in the |
|
1060 | give (list/tuple/str) then the variable values looked up in the | |
1059 | callers frame. |
|
1061 | callers frame. | |
1060 | interactive : bool |
|
1062 | interactive : bool | |
1061 | If True (default), the variables will be listed with the ``who`` |
|
1063 | If True (default), the variables will be listed with the ``who`` | |
1062 | magic. |
|
1064 | magic. | |
1063 | """ |
|
1065 | """ | |
1064 | vdict = None |
|
1066 | vdict = None | |
1065 |
|
1067 | |||
1066 | # We need a dict of name/value pairs to do namespace updates. |
|
1068 | # We need a dict of name/value pairs to do namespace updates. | |
1067 | if isinstance(variables, dict): |
|
1069 | if isinstance(variables, dict): | |
1068 | vdict = variables |
|
1070 | vdict = variables | |
1069 | elif isinstance(variables, (basestring, list, tuple)): |
|
1071 | elif isinstance(variables, (basestring, list, tuple)): | |
1070 | if isinstance(variables, basestring): |
|
1072 | if isinstance(variables, basestring): | |
1071 | vlist = variables.split() |
|
1073 | vlist = variables.split() | |
1072 | else: |
|
1074 | else: | |
1073 | vlist = variables |
|
1075 | vlist = variables | |
1074 | vdict = {} |
|
1076 | vdict = {} | |
1075 | cf = sys._getframe(1) |
|
1077 | cf = sys._getframe(1) | |
1076 | for name in vlist: |
|
1078 | for name in vlist: | |
1077 | try: |
|
1079 | try: | |
1078 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1080 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1079 | except: |
|
1081 | except: | |
1080 | print ('Could not get variable %s from %s' % |
|
1082 | print ('Could not get variable %s from %s' % | |
1081 | (name,cf.f_code.co_name)) |
|
1083 | (name,cf.f_code.co_name)) | |
1082 | else: |
|
1084 | else: | |
1083 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1085 | raise ValueError('variables must be a dict/str/list/tuple') | |
1084 |
|
1086 | |||
1085 | # Propagate variables to user namespace |
|
1087 | # Propagate variables to user namespace | |
1086 | self.user_ns.update(vdict) |
|
1088 | self.user_ns.update(vdict) | |
1087 |
|
1089 | |||
1088 | # And configure interactive visibility |
|
1090 | # And configure interactive visibility | |
1089 | config_ns = self.user_ns_hidden |
|
1091 | config_ns = self.user_ns_hidden | |
1090 | if interactive: |
|
1092 | if interactive: | |
1091 | for name, val in vdict.iteritems(): |
|
1093 | for name, val in vdict.iteritems(): | |
1092 | config_ns.pop(name, None) |
|
1094 | config_ns.pop(name, None) | |
1093 | else: |
|
1095 | else: | |
1094 | for name,val in vdict.iteritems(): |
|
1096 | for name,val in vdict.iteritems(): | |
1095 | config_ns[name] = val |
|
1097 | config_ns[name] = val | |
1096 |
|
1098 | |||
1097 | #------------------------------------------------------------------------- |
|
1099 | #------------------------------------------------------------------------- | |
1098 | # Things related to object introspection |
|
1100 | # Things related to object introspection | |
1099 | #------------------------------------------------------------------------- |
|
1101 | #------------------------------------------------------------------------- | |
1100 |
|
1102 | |||
1101 | def _ofind(self, oname, namespaces=None): |
|
1103 | def _ofind(self, oname, namespaces=None): | |
1102 | """Find an object in the available namespaces. |
|
1104 | """Find an object in the available namespaces. | |
1103 |
|
1105 | |||
1104 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1106 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1105 |
|
1107 | |||
1106 | Has special code to detect magic functions. |
|
1108 | Has special code to detect magic functions. | |
1107 | """ |
|
1109 | """ | |
1108 | #oname = oname.strip() |
|
1110 | #oname = oname.strip() | |
1109 | #print '1- oname: <%r>' % oname # dbg |
|
1111 | #print '1- oname: <%r>' % oname # dbg | |
1110 | try: |
|
1112 | try: | |
1111 | oname = oname.strip().encode('ascii') |
|
1113 | oname = oname.strip().encode('ascii') | |
1112 | #print '2- oname: <%r>' % oname # dbg |
|
1114 | #print '2- oname: <%r>' % oname # dbg | |
1113 | except UnicodeEncodeError: |
|
1115 | except UnicodeEncodeError: | |
1114 | print 'Python identifiers can only contain ascii characters.' |
|
1116 | print 'Python identifiers can only contain ascii characters.' | |
1115 | return dict(found=False) |
|
1117 | return dict(found=False) | |
1116 |
|
1118 | |||
1117 | alias_ns = None |
|
1119 | alias_ns = None | |
1118 | if namespaces is None: |
|
1120 | if namespaces is None: | |
1119 | # Namespaces to search in: |
|
1121 | # Namespaces to search in: | |
1120 | # Put them in a list. The order is important so that we |
|
1122 | # Put them in a list. The order is important so that we | |
1121 | # find things in the same order that Python finds them. |
|
1123 | # find things in the same order that Python finds them. | |
1122 | namespaces = [ ('Interactive', self.user_ns), |
|
1124 | namespaces = [ ('Interactive', self.user_ns), | |
1123 | ('IPython internal', self.internal_ns), |
|
1125 | ('IPython internal', self.internal_ns), | |
1124 | ('Python builtin', __builtin__.__dict__), |
|
1126 | ('Python builtin', __builtin__.__dict__), | |
1125 | ('Alias', self.alias_manager.alias_table), |
|
1127 | ('Alias', self.alias_manager.alias_table), | |
1126 | ] |
|
1128 | ] | |
1127 | alias_ns = self.alias_manager.alias_table |
|
1129 | alias_ns = self.alias_manager.alias_table | |
1128 |
|
1130 | |||
1129 | # initialize results to 'null' |
|
1131 | # initialize results to 'null' | |
1130 | found = False; obj = None; ospace = None; ds = None; |
|
1132 | found = False; obj = None; ospace = None; ds = None; | |
1131 | ismagic = False; isalias = False; parent = None |
|
1133 | ismagic = False; isalias = False; parent = None | |
1132 |
|
1134 | |||
1133 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1135 | # We need to special-case 'print', which as of python2.6 registers as a | |
1134 | # function but should only be treated as one if print_function was |
|
1136 | # function but should only be treated as one if print_function was | |
1135 | # loaded with a future import. In this case, just bail. |
|
1137 | # loaded with a future import. In this case, just bail. | |
1136 | if (oname == 'print' and not (self.compile.compiler_flags & |
|
1138 | if (oname == 'print' and not (self.compile.compiler_flags & | |
1137 | __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1139 | __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1138 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1140 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1139 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1141 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1140 |
|
1142 | |||
1141 | # Look for the given name by splitting it in parts. If the head is |
|
1143 | # Look for the given name by splitting it in parts. If the head is | |
1142 | # found, then we look for all the remaining parts as members, and only |
|
1144 | # found, then we look for all the remaining parts as members, and only | |
1143 | # declare success if we can find them all. |
|
1145 | # declare success if we can find them all. | |
1144 | oname_parts = oname.split('.') |
|
1146 | oname_parts = oname.split('.') | |
1145 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1147 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1146 | for nsname,ns in namespaces: |
|
1148 | for nsname,ns in namespaces: | |
1147 | try: |
|
1149 | try: | |
1148 | obj = ns[oname_head] |
|
1150 | obj = ns[oname_head] | |
1149 | except KeyError: |
|
1151 | except KeyError: | |
1150 | continue |
|
1152 | continue | |
1151 | else: |
|
1153 | else: | |
1152 | #print 'oname_rest:', oname_rest # dbg |
|
1154 | #print 'oname_rest:', oname_rest # dbg | |
1153 | for part in oname_rest: |
|
1155 | for part in oname_rest: | |
1154 | try: |
|
1156 | try: | |
1155 | parent = obj |
|
1157 | parent = obj | |
1156 | obj = getattr(obj,part) |
|
1158 | obj = getattr(obj,part) | |
1157 | except: |
|
1159 | except: | |
1158 | # Blanket except b/c some badly implemented objects |
|
1160 | # Blanket except b/c some badly implemented objects | |
1159 | # allow __getattr__ to raise exceptions other than |
|
1161 | # allow __getattr__ to raise exceptions other than | |
1160 | # AttributeError, which then crashes IPython. |
|
1162 | # AttributeError, which then crashes IPython. | |
1161 | break |
|
1163 | break | |
1162 | else: |
|
1164 | else: | |
1163 | # If we finish the for loop (no break), we got all members |
|
1165 | # If we finish the for loop (no break), we got all members | |
1164 | found = True |
|
1166 | found = True | |
1165 | ospace = nsname |
|
1167 | ospace = nsname | |
1166 | if ns == alias_ns: |
|
1168 | if ns == alias_ns: | |
1167 | isalias = True |
|
1169 | isalias = True | |
1168 | break # namespace loop |
|
1170 | break # namespace loop | |
1169 |
|
1171 | |||
1170 | # Try to see if it's magic |
|
1172 | # Try to see if it's magic | |
1171 | if not found: |
|
1173 | if not found: | |
1172 | if oname.startswith(ESC_MAGIC): |
|
1174 | if oname.startswith(ESC_MAGIC): | |
1173 | oname = oname[1:] |
|
1175 | oname = oname[1:] | |
1174 | obj = getattr(self,'magic_'+oname,None) |
|
1176 | obj = getattr(self,'magic_'+oname,None) | |
1175 | if obj is not None: |
|
1177 | if obj is not None: | |
1176 | found = True |
|
1178 | found = True | |
1177 | ospace = 'IPython internal' |
|
1179 | ospace = 'IPython internal' | |
1178 | ismagic = True |
|
1180 | ismagic = True | |
1179 |
|
1181 | |||
1180 | # Last try: special-case some literals like '', [], {}, etc: |
|
1182 | # Last try: special-case some literals like '', [], {}, etc: | |
1181 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1183 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1182 | obj = eval(oname_head) |
|
1184 | obj = eval(oname_head) | |
1183 | found = True |
|
1185 | found = True | |
1184 | ospace = 'Interactive' |
|
1186 | ospace = 'Interactive' | |
1185 |
|
1187 | |||
1186 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1188 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1187 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1189 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1188 |
|
1190 | |||
1189 | def _ofind_property(self, oname, info): |
|
1191 | def _ofind_property(self, oname, info): | |
1190 | """Second part of object finding, to look for property details.""" |
|
1192 | """Second part of object finding, to look for property details.""" | |
1191 | if info.found: |
|
1193 | if info.found: | |
1192 | # Get the docstring of the class property if it exists. |
|
1194 | # Get the docstring of the class property if it exists. | |
1193 | path = oname.split('.') |
|
1195 | path = oname.split('.') | |
1194 | root = '.'.join(path[:-1]) |
|
1196 | root = '.'.join(path[:-1]) | |
1195 | if info.parent is not None: |
|
1197 | if info.parent is not None: | |
1196 | try: |
|
1198 | try: | |
1197 | target = getattr(info.parent, '__class__') |
|
1199 | target = getattr(info.parent, '__class__') | |
1198 | # The object belongs to a class instance. |
|
1200 | # The object belongs to a class instance. | |
1199 | try: |
|
1201 | try: | |
1200 | target = getattr(target, path[-1]) |
|
1202 | target = getattr(target, path[-1]) | |
1201 | # The class defines the object. |
|
1203 | # The class defines the object. | |
1202 | if isinstance(target, property): |
|
1204 | if isinstance(target, property): | |
1203 | oname = root + '.__class__.' + path[-1] |
|
1205 | oname = root + '.__class__.' + path[-1] | |
1204 | info = Struct(self._ofind(oname)) |
|
1206 | info = Struct(self._ofind(oname)) | |
1205 | except AttributeError: pass |
|
1207 | except AttributeError: pass | |
1206 | except AttributeError: pass |
|
1208 | except AttributeError: pass | |
1207 |
|
1209 | |||
1208 | # We return either the new info or the unmodified input if the object |
|
1210 | # We return either the new info or the unmodified input if the object | |
1209 | # hadn't been found |
|
1211 | # hadn't been found | |
1210 | return info |
|
1212 | return info | |
1211 |
|
1213 | |||
1212 | def _object_find(self, oname, namespaces=None): |
|
1214 | def _object_find(self, oname, namespaces=None): | |
1213 | """Find an object and return a struct with info about it.""" |
|
1215 | """Find an object and return a struct with info about it.""" | |
1214 | inf = Struct(self._ofind(oname, namespaces)) |
|
1216 | inf = Struct(self._ofind(oname, namespaces)) | |
1215 | return Struct(self._ofind_property(oname, inf)) |
|
1217 | return Struct(self._ofind_property(oname, inf)) | |
1216 |
|
1218 | |||
1217 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1219 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1218 | """Generic interface to the inspector system. |
|
1220 | """Generic interface to the inspector system. | |
1219 |
|
1221 | |||
1220 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1222 | This function is meant to be called by pdef, pdoc & friends.""" | |
1221 | info = self._object_find(oname) |
|
1223 | info = self._object_find(oname) | |
1222 | if info.found: |
|
1224 | if info.found: | |
1223 | pmethod = getattr(self.inspector, meth) |
|
1225 | pmethod = getattr(self.inspector, meth) | |
1224 | formatter = format_screen if info.ismagic else None |
|
1226 | formatter = format_screen if info.ismagic else None | |
1225 | if meth == 'pdoc': |
|
1227 | if meth == 'pdoc': | |
1226 | pmethod(info.obj, oname, formatter) |
|
1228 | pmethod(info.obj, oname, formatter) | |
1227 | elif meth == 'pinfo': |
|
1229 | elif meth == 'pinfo': | |
1228 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1230 | pmethod(info.obj, oname, formatter, info, **kw) | |
1229 | else: |
|
1231 | else: | |
1230 | pmethod(info.obj, oname) |
|
1232 | pmethod(info.obj, oname) | |
1231 | else: |
|
1233 | else: | |
1232 | print 'Object `%s` not found.' % oname |
|
1234 | print 'Object `%s` not found.' % oname | |
1233 | return 'not found' # so callers can take other action |
|
1235 | return 'not found' # so callers can take other action | |
1234 |
|
1236 | |||
1235 | def object_inspect(self, oname): |
|
1237 | def object_inspect(self, oname): | |
1236 | info = self._object_find(oname) |
|
1238 | info = self._object_find(oname) | |
1237 | if info.found: |
|
1239 | if info.found: | |
1238 | return self.inspector.info(info.obj, oname, info=info) |
|
1240 | return self.inspector.info(info.obj, oname, info=info) | |
1239 | else: |
|
1241 | else: | |
1240 | return oinspect.object_info(name=oname, found=False) |
|
1242 | return oinspect.object_info(name=oname, found=False) | |
1241 |
|
1243 | |||
1242 | #------------------------------------------------------------------------- |
|
1244 | #------------------------------------------------------------------------- | |
1243 | # Things related to history management |
|
1245 | # Things related to history management | |
1244 | #------------------------------------------------------------------------- |
|
1246 | #------------------------------------------------------------------------- | |
1245 |
|
1247 | |||
1246 | def init_history(self): |
|
1248 | def init_history(self): | |
1247 | """Sets up the command history, and starts regular autosaves.""" |
|
1249 | """Sets up the command history, and starts regular autosaves.""" | |
1248 | self.history_manager = HistoryManager(shell=self, config=self.config) |
|
1250 | self.history_manager = HistoryManager(shell=self, config=self.config) | |
1249 |
|
1251 | |||
1250 | def history_saving_wrapper(self, func): |
|
1252 | def history_saving_wrapper(self, func): | |
1251 | """ Wrap func for readline history saving |
|
1253 | """ Wrap func for readline history saving | |
1252 |
|
1254 | |||
1253 | Convert func into callable that saves & restores |
|
1255 | Convert func into callable that saves & restores | |
1254 | history around the call """ |
|
1256 | history around the call """ | |
1255 |
|
1257 | |||
1256 | if self.has_readline: |
|
1258 | if self.has_readline: | |
1257 | from IPython.utils import rlineimpl as readline |
|
1259 | from IPython.utils import rlineimpl as readline | |
1258 | else: |
|
1260 | else: | |
1259 | return func |
|
1261 | return func | |
1260 |
|
1262 | |||
1261 | def wrapper(): |
|
1263 | def wrapper(): | |
1262 | self.save_history() |
|
1264 | self.save_history() | |
1263 | try: |
|
1265 | try: | |
1264 | func() |
|
1266 | func() | |
1265 | finally: |
|
1267 | finally: | |
1266 | self.reload_history() |
|
1268 | self.reload_history() | |
1267 | return wrapper |
|
1269 | return wrapper | |
1268 |
|
1270 | |||
1269 | def get_history(self, start=1, stop=None, raw=False, output=True): |
|
|||
1270 | return self.history_manager.get_history(start, stop, raw, output) |
|
|||
1271 |
|
||||
1272 |
|
1271 | |||
1273 | #------------------------------------------------------------------------- |
|
1272 | #------------------------------------------------------------------------- | |
1274 | # Things related to exception handling and tracebacks (not debugging) |
|
1273 | # Things related to exception handling and tracebacks (not debugging) | |
1275 | #------------------------------------------------------------------------- |
|
1274 | #------------------------------------------------------------------------- | |
1276 |
|
1275 | |||
1277 | def init_traceback_handlers(self, custom_exceptions): |
|
1276 | def init_traceback_handlers(self, custom_exceptions): | |
1278 | # Syntax error handler. |
|
1277 | # Syntax error handler. | |
1279 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1278 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1280 |
|
1279 | |||
1281 | # The interactive one is initialized with an offset, meaning we always |
|
1280 | # The interactive one is initialized with an offset, meaning we always | |
1282 | # want to remove the topmost item in the traceback, which is our own |
|
1281 | # want to remove the topmost item in the traceback, which is our own | |
1283 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1282 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1284 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1283 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1285 | color_scheme='NoColor', |
|
1284 | color_scheme='NoColor', | |
1286 | tb_offset = 1, |
|
1285 | tb_offset = 1, | |
1287 | check_cache=self.compile.check_cache) |
|
1286 | check_cache=self.compile.check_cache) | |
1288 |
|
1287 | |||
1289 | # The instance will store a pointer to the system-wide exception hook, |
|
1288 | # The instance will store a pointer to the system-wide exception hook, | |
1290 | # so that runtime code (such as magics) can access it. This is because |
|
1289 | # so that runtime code (such as magics) can access it. This is because | |
1291 | # during the read-eval loop, it may get temporarily overwritten. |
|
1290 | # during the read-eval loop, it may get temporarily overwritten. | |
1292 | self.sys_excepthook = sys.excepthook |
|
1291 | self.sys_excepthook = sys.excepthook | |
1293 |
|
1292 | |||
1294 | # and add any custom exception handlers the user may have specified |
|
1293 | # and add any custom exception handlers the user may have specified | |
1295 | self.set_custom_exc(*custom_exceptions) |
|
1294 | self.set_custom_exc(*custom_exceptions) | |
1296 |
|
1295 | |||
1297 | # Set the exception mode |
|
1296 | # Set the exception mode | |
1298 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1297 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1299 |
|
1298 | |||
1300 | def set_custom_exc(self, exc_tuple, handler): |
|
1299 | def set_custom_exc(self, exc_tuple, handler): | |
1301 | """set_custom_exc(exc_tuple,handler) |
|
1300 | """set_custom_exc(exc_tuple,handler) | |
1302 |
|
1301 | |||
1303 | Set a custom exception handler, which will be called if any of the |
|
1302 | Set a custom exception handler, which will be called if any of the | |
1304 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1303 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1305 | run_code() method. |
|
1304 | run_code() method. | |
1306 |
|
1305 | |||
1307 | Inputs: |
|
1306 | Inputs: | |
1308 |
|
1307 | |||
1309 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1308 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
1310 | handler for. It is very important that you use a tuple, and NOT A |
|
1309 | handler for. It is very important that you use a tuple, and NOT A | |
1311 | LIST here, because of the way Python's except statement works. If |
|
1310 | LIST here, because of the way Python's except statement works. If | |
1312 | you only want to trap a single exception, use a singleton tuple: |
|
1311 | you only want to trap a single exception, use a singleton tuple: | |
1313 |
|
1312 | |||
1314 | exc_tuple == (MyCustomException,) |
|
1313 | exc_tuple == (MyCustomException,) | |
1315 |
|
1314 | |||
1316 | - handler: this must be defined as a function with the following |
|
1315 | - handler: this must be defined as a function with the following | |
1317 | basic interface:: |
|
1316 | basic interface:: | |
1318 |
|
1317 | |||
1319 | def my_handler(self, etype, value, tb, tb_offset=None) |
|
1318 | def my_handler(self, etype, value, tb, tb_offset=None) | |
1320 | ... |
|
1319 | ... | |
1321 | # The return value must be |
|
1320 | # The return value must be | |
1322 | return structured_traceback |
|
1321 | return structured_traceback | |
1323 |
|
1322 | |||
1324 | This will be made into an instance method (via types.MethodType) |
|
1323 | This will be made into an instance method (via types.MethodType) | |
1325 | of IPython itself, and it will be called if any of the exceptions |
|
1324 | of IPython itself, and it will be called if any of the exceptions | |
1326 | listed in the exc_tuple are caught. If the handler is None, an |
|
1325 | listed in the exc_tuple are caught. If the handler is None, an | |
1327 | internal basic one is used, which just prints basic info. |
|
1326 | internal basic one is used, which just prints basic info. | |
1328 |
|
1327 | |||
1329 | WARNING: by putting in your own exception handler into IPython's main |
|
1328 | WARNING: by putting in your own exception handler into IPython's main | |
1330 | execution loop, you run a very good chance of nasty crashes. This |
|
1329 | execution loop, you run a very good chance of nasty crashes. This | |
1331 | facility should only be used if you really know what you are doing.""" |
|
1330 | facility should only be used if you really know what you are doing.""" | |
1332 |
|
1331 | |||
1333 | assert type(exc_tuple)==type(()) , \ |
|
1332 | assert type(exc_tuple)==type(()) , \ | |
1334 | "The custom exceptions must be given AS A TUPLE." |
|
1333 | "The custom exceptions must be given AS A TUPLE." | |
1335 |
|
1334 | |||
1336 | def dummy_handler(self,etype,value,tb): |
|
1335 | def dummy_handler(self,etype,value,tb): | |
1337 | print '*** Simple custom exception handler ***' |
|
1336 | print '*** Simple custom exception handler ***' | |
1338 | print 'Exception type :',etype |
|
1337 | print 'Exception type :',etype | |
1339 | print 'Exception value:',value |
|
1338 | print 'Exception value:',value | |
1340 | print 'Traceback :',tb |
|
1339 | print 'Traceback :',tb | |
1341 | print 'Source code :','\n'.join(self.buffer) |
|
1340 | print 'Source code :','\n'.join(self.buffer) | |
1342 |
|
1341 | |||
1343 | if handler is None: handler = dummy_handler |
|
1342 | if handler is None: handler = dummy_handler | |
1344 |
|
1343 | |||
1345 | self.CustomTB = types.MethodType(handler,self) |
|
1344 | self.CustomTB = types.MethodType(handler,self) | |
1346 | self.custom_exceptions = exc_tuple |
|
1345 | self.custom_exceptions = exc_tuple | |
1347 |
|
1346 | |||
1348 | def excepthook(self, etype, value, tb): |
|
1347 | def excepthook(self, etype, value, tb): | |
1349 | """One more defense for GUI apps that call sys.excepthook. |
|
1348 | """One more defense for GUI apps that call sys.excepthook. | |
1350 |
|
1349 | |||
1351 | GUI frameworks like wxPython trap exceptions and call |
|
1350 | GUI frameworks like wxPython trap exceptions and call | |
1352 | sys.excepthook themselves. I guess this is a feature that |
|
1351 | sys.excepthook themselves. I guess this is a feature that | |
1353 | enables them to keep running after exceptions that would |
|
1352 | enables them to keep running after exceptions that would | |
1354 | otherwise kill their mainloop. This is a bother for IPython |
|
1353 | otherwise kill their mainloop. This is a bother for IPython | |
1355 | which excepts to catch all of the program exceptions with a try: |
|
1354 | which excepts to catch all of the program exceptions with a try: | |
1356 | except: statement. |
|
1355 | except: statement. | |
1357 |
|
1356 | |||
1358 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1357 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1359 | any app directly invokes sys.excepthook, it will look to the user like |
|
1358 | any app directly invokes sys.excepthook, it will look to the user like | |
1360 | IPython crashed. In order to work around this, we can disable the |
|
1359 | IPython crashed. In order to work around this, we can disable the | |
1361 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1360 | CrashHandler and replace it with this excepthook instead, which prints a | |
1362 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1361 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1363 | call sys.excepthook will generate a regular-looking exception from |
|
1362 | call sys.excepthook will generate a regular-looking exception from | |
1364 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1363 | IPython, and the CrashHandler will only be triggered by real IPython | |
1365 | crashes. |
|
1364 | crashes. | |
1366 |
|
1365 | |||
1367 | This hook should be used sparingly, only in places which are not likely |
|
1366 | This hook should be used sparingly, only in places which are not likely | |
1368 | to be true IPython errors. |
|
1367 | to be true IPython errors. | |
1369 | """ |
|
1368 | """ | |
1370 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1369 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1371 |
|
1370 | |||
1372 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1371 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | |
1373 | exception_only=False): |
|
1372 | exception_only=False): | |
1374 | """Display the exception that just occurred. |
|
1373 | """Display the exception that just occurred. | |
1375 |
|
1374 | |||
1376 | If nothing is known about the exception, this is the method which |
|
1375 | If nothing is known about the exception, this is the method which | |
1377 | should be used throughout the code for presenting user tracebacks, |
|
1376 | should be used throughout the code for presenting user tracebacks, | |
1378 | rather than directly invoking the InteractiveTB object. |
|
1377 | rather than directly invoking the InteractiveTB object. | |
1379 |
|
1378 | |||
1380 | A specific showsyntaxerror() also exists, but this method can take |
|
1379 | A specific showsyntaxerror() also exists, but this method can take | |
1381 | care of calling it if needed, so unless you are explicitly catching a |
|
1380 | care of calling it if needed, so unless you are explicitly catching a | |
1382 | SyntaxError exception, don't try to analyze the stack manually and |
|
1381 | SyntaxError exception, don't try to analyze the stack manually and | |
1383 | simply call this method.""" |
|
1382 | simply call this method.""" | |
1384 |
|
1383 | |||
1385 | try: |
|
1384 | try: | |
1386 | if exc_tuple is None: |
|
1385 | if exc_tuple is None: | |
1387 | etype, value, tb = sys.exc_info() |
|
1386 | etype, value, tb = sys.exc_info() | |
1388 | else: |
|
1387 | else: | |
1389 | etype, value, tb = exc_tuple |
|
1388 | etype, value, tb = exc_tuple | |
1390 |
|
1389 | |||
1391 | if etype is None: |
|
1390 | if etype is None: | |
1392 | if hasattr(sys, 'last_type'): |
|
1391 | if hasattr(sys, 'last_type'): | |
1393 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1392 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1394 | sys.last_traceback |
|
1393 | sys.last_traceback | |
1395 | else: |
|
1394 | else: | |
1396 | self.write_err('No traceback available to show.\n') |
|
1395 | self.write_err('No traceback available to show.\n') | |
1397 | return |
|
1396 | return | |
1398 |
|
1397 | |||
1399 | if etype is SyntaxError: |
|
1398 | if etype is SyntaxError: | |
1400 | # Though this won't be called by syntax errors in the input |
|
1399 | # Though this won't be called by syntax errors in the input | |
1401 | # line, there may be SyntaxError cases whith imported code. |
|
1400 | # line, there may be SyntaxError cases whith imported code. | |
1402 | self.showsyntaxerror(filename) |
|
1401 | self.showsyntaxerror(filename) | |
1403 | elif etype is UsageError: |
|
1402 | elif etype is UsageError: | |
1404 | print "UsageError:", value |
|
1403 | print "UsageError:", value | |
1405 | else: |
|
1404 | else: | |
1406 | # WARNING: these variables are somewhat deprecated and not |
|
1405 | # WARNING: these variables are somewhat deprecated and not | |
1407 | # necessarily safe to use in a threaded environment, but tools |
|
1406 | # necessarily safe to use in a threaded environment, but tools | |
1408 | # like pdb depend on their existence, so let's set them. If we |
|
1407 | # like pdb depend on their existence, so let's set them. If we | |
1409 | # find problems in the field, we'll need to revisit their use. |
|
1408 | # find problems in the field, we'll need to revisit their use. | |
1410 | sys.last_type = etype |
|
1409 | sys.last_type = etype | |
1411 | sys.last_value = value |
|
1410 | sys.last_value = value | |
1412 | sys.last_traceback = tb |
|
1411 | sys.last_traceback = tb | |
1413 |
|
1412 | |||
1414 | if etype in self.custom_exceptions: |
|
1413 | if etype in self.custom_exceptions: | |
1415 | # FIXME: Old custom traceback objects may just return a |
|
1414 | # FIXME: Old custom traceback objects may just return a | |
1416 | # string, in that case we just put it into a list |
|
1415 | # string, in that case we just put it into a list | |
1417 | stb = self.CustomTB(etype, value, tb, tb_offset) |
|
1416 | stb = self.CustomTB(etype, value, tb, tb_offset) | |
1418 | if isinstance(ctb, basestring): |
|
1417 | if isinstance(ctb, basestring): | |
1419 | stb = [stb] |
|
1418 | stb = [stb] | |
1420 | else: |
|
1419 | else: | |
1421 | if exception_only: |
|
1420 | if exception_only: | |
1422 | stb = ['An exception has occurred, use %tb to see ' |
|
1421 | stb = ['An exception has occurred, use %tb to see ' | |
1423 | 'the full traceback.\n'] |
|
1422 | 'the full traceback.\n'] | |
1424 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1423 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1425 | value)) |
|
1424 | value)) | |
1426 | else: |
|
1425 | else: | |
1427 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1426 | stb = self.InteractiveTB.structured_traceback(etype, | |
1428 | value, tb, tb_offset=tb_offset) |
|
1427 | value, tb, tb_offset=tb_offset) | |
1429 | # FIXME: the pdb calling should be done by us, not by |
|
1428 | # FIXME: the pdb calling should be done by us, not by | |
1430 | # the code computing the traceback. |
|
1429 | # the code computing the traceback. | |
1431 | if self.InteractiveTB.call_pdb: |
|
1430 | if self.InteractiveTB.call_pdb: | |
1432 | # pdb mucks up readline, fix it back |
|
1431 | # pdb mucks up readline, fix it back | |
1433 | self.set_readline_completer() |
|
1432 | self.set_readline_completer() | |
1434 |
|
1433 | |||
1435 | # Actually show the traceback |
|
1434 | # Actually show the traceback | |
1436 | self._showtraceback(etype, value, stb) |
|
1435 | self._showtraceback(etype, value, stb) | |
1437 |
|
1436 | |||
1438 | except KeyboardInterrupt: |
|
1437 | except KeyboardInterrupt: | |
1439 | self.write_err("\nKeyboardInterrupt\n") |
|
1438 | self.write_err("\nKeyboardInterrupt\n") | |
1440 |
|
1439 | |||
1441 | def _showtraceback(self, etype, evalue, stb): |
|
1440 | def _showtraceback(self, etype, evalue, stb): | |
1442 | """Actually show a traceback. |
|
1441 | """Actually show a traceback. | |
1443 |
|
1442 | |||
1444 | Subclasses may override this method to put the traceback on a different |
|
1443 | Subclasses may override this method to put the traceback on a different | |
1445 | place, like a side channel. |
|
1444 | place, like a side channel. | |
1446 | """ |
|
1445 | """ | |
1447 | print >> io.Term.cout, self.InteractiveTB.stb2text(stb) |
|
1446 | print >> io.Term.cout, self.InteractiveTB.stb2text(stb) | |
1448 |
|
1447 | |||
1449 | def showsyntaxerror(self, filename=None): |
|
1448 | def showsyntaxerror(self, filename=None): | |
1450 | """Display the syntax error that just occurred. |
|
1449 | """Display the syntax error that just occurred. | |
1451 |
|
1450 | |||
1452 | This doesn't display a stack trace because there isn't one. |
|
1451 | This doesn't display a stack trace because there isn't one. | |
1453 |
|
1452 | |||
1454 | If a filename is given, it is stuffed in the exception instead |
|
1453 | If a filename is given, it is stuffed in the exception instead | |
1455 | of what was there before (because Python's parser always uses |
|
1454 | of what was there before (because Python's parser always uses | |
1456 | "<string>" when reading from a string). |
|
1455 | "<string>" when reading from a string). | |
1457 | """ |
|
1456 | """ | |
1458 | etype, value, last_traceback = sys.exc_info() |
|
1457 | etype, value, last_traceback = sys.exc_info() | |
1459 |
|
1458 | |||
1460 | # See note about these variables in showtraceback() above |
|
1459 | # See note about these variables in showtraceback() above | |
1461 | sys.last_type = etype |
|
1460 | sys.last_type = etype | |
1462 | sys.last_value = value |
|
1461 | sys.last_value = value | |
1463 | sys.last_traceback = last_traceback |
|
1462 | sys.last_traceback = last_traceback | |
1464 |
|
1463 | |||
1465 | if filename and etype is SyntaxError: |
|
1464 | if filename and etype is SyntaxError: | |
1466 | # Work hard to stuff the correct filename in the exception |
|
1465 | # Work hard to stuff the correct filename in the exception | |
1467 | try: |
|
1466 | try: | |
1468 | msg, (dummy_filename, lineno, offset, line) = value |
|
1467 | msg, (dummy_filename, lineno, offset, line) = value | |
1469 | except: |
|
1468 | except: | |
1470 | # Not the format we expect; leave it alone |
|
1469 | # Not the format we expect; leave it alone | |
1471 | pass |
|
1470 | pass | |
1472 | else: |
|
1471 | else: | |
1473 | # Stuff in the right filename |
|
1472 | # Stuff in the right filename | |
1474 | try: |
|
1473 | try: | |
1475 | # Assume SyntaxError is a class exception |
|
1474 | # Assume SyntaxError is a class exception | |
1476 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1475 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1477 | except: |
|
1476 | except: | |
1478 | # If that failed, assume SyntaxError is a string |
|
1477 | # If that failed, assume SyntaxError is a string | |
1479 | value = msg, (filename, lineno, offset, line) |
|
1478 | value = msg, (filename, lineno, offset, line) | |
1480 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1479 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1481 | self._showtraceback(etype, value, stb) |
|
1480 | self._showtraceback(etype, value, stb) | |
1482 |
|
1481 | |||
1483 | #------------------------------------------------------------------------- |
|
1482 | #------------------------------------------------------------------------- | |
1484 | # Things related to readline |
|
1483 | # Things related to readline | |
1485 | #------------------------------------------------------------------------- |
|
1484 | #------------------------------------------------------------------------- | |
1486 |
|
1485 | |||
1487 | def init_readline(self): |
|
1486 | def init_readline(self): | |
1488 | """Command history completion/saving/reloading.""" |
|
1487 | """Command history completion/saving/reloading.""" | |
1489 |
|
1488 | |||
1490 | if self.readline_use: |
|
1489 | if self.readline_use: | |
1491 | import IPython.utils.rlineimpl as readline |
|
1490 | import IPython.utils.rlineimpl as readline | |
1492 |
|
1491 | |||
1493 | self.rl_next_input = None |
|
1492 | self.rl_next_input = None | |
1494 | self.rl_do_indent = False |
|
1493 | self.rl_do_indent = False | |
1495 |
|
1494 | |||
1496 | if not self.readline_use or not readline.have_readline: |
|
1495 | if not self.readline_use or not readline.have_readline: | |
1497 | self.has_readline = False |
|
1496 | self.has_readline = False | |
1498 | self.readline = None |
|
1497 | self.readline = None | |
1499 | # Set a number of methods that depend on readline to be no-op |
|
1498 | # Set a number of methods that depend on readline to be no-op | |
1500 | self.set_readline_completer = no_op |
|
1499 | self.set_readline_completer = no_op | |
1501 | self.set_custom_completer = no_op |
|
1500 | self.set_custom_completer = no_op | |
1502 | self.set_completer_frame = no_op |
|
1501 | self.set_completer_frame = no_op | |
1503 | warn('Readline services not available or not loaded.') |
|
1502 | warn('Readline services not available or not loaded.') | |
1504 | else: |
|
1503 | else: | |
1505 | self.has_readline = True |
|
1504 | self.has_readline = True | |
1506 | self.readline = readline |
|
1505 | self.readline = readline | |
1507 | sys.modules['readline'] = readline |
|
1506 | sys.modules['readline'] = readline | |
1508 |
|
1507 | |||
1509 | # Platform-specific configuration |
|
1508 | # Platform-specific configuration | |
1510 | if os.name == 'nt': |
|
1509 | if os.name == 'nt': | |
1511 | # FIXME - check with Frederick to see if we can harmonize |
|
1510 | # FIXME - check with Frederick to see if we can harmonize | |
1512 | # naming conventions with pyreadline to avoid this |
|
1511 | # naming conventions with pyreadline to avoid this | |
1513 | # platform-dependent check |
|
1512 | # platform-dependent check | |
1514 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1513 | self.readline_startup_hook = readline.set_pre_input_hook | |
1515 | else: |
|
1514 | else: | |
1516 | self.readline_startup_hook = readline.set_startup_hook |
|
1515 | self.readline_startup_hook = readline.set_startup_hook | |
1517 |
|
1516 | |||
1518 | # Load user's initrc file (readline config) |
|
1517 | # Load user's initrc file (readline config) | |
1519 | # Or if libedit is used, load editrc. |
|
1518 | # Or if libedit is used, load editrc. | |
1520 | inputrc_name = os.environ.get('INPUTRC') |
|
1519 | inputrc_name = os.environ.get('INPUTRC') | |
1521 | if inputrc_name is None: |
|
1520 | if inputrc_name is None: | |
1522 | home_dir = get_home_dir() |
|
1521 | home_dir = get_home_dir() | |
1523 | if home_dir is not None: |
|
1522 | if home_dir is not None: | |
1524 | inputrc_name = '.inputrc' |
|
1523 | inputrc_name = '.inputrc' | |
1525 | if readline.uses_libedit: |
|
1524 | if readline.uses_libedit: | |
1526 | inputrc_name = '.editrc' |
|
1525 | inputrc_name = '.editrc' | |
1527 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1526 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
1528 | if os.path.isfile(inputrc_name): |
|
1527 | if os.path.isfile(inputrc_name): | |
1529 | try: |
|
1528 | try: | |
1530 | readline.read_init_file(inputrc_name) |
|
1529 | readline.read_init_file(inputrc_name) | |
1531 | except: |
|
1530 | except: | |
1532 | warn('Problems reading readline initialization file <%s>' |
|
1531 | warn('Problems reading readline initialization file <%s>' | |
1533 | % inputrc_name) |
|
1532 | % inputrc_name) | |
1534 |
|
1533 | |||
1535 | # Configure readline according to user's prefs |
|
1534 | # Configure readline according to user's prefs | |
1536 | # This is only done if GNU readline is being used. If libedit |
|
1535 | # This is only done if GNU readline is being used. If libedit | |
1537 | # is being used (as on Leopard) the readline config is |
|
1536 | # is being used (as on Leopard) the readline config is | |
1538 | # not run as the syntax for libedit is different. |
|
1537 | # not run as the syntax for libedit is different. | |
1539 | if not readline.uses_libedit: |
|
1538 | if not readline.uses_libedit: | |
1540 | for rlcommand in self.readline_parse_and_bind: |
|
1539 | for rlcommand in self.readline_parse_and_bind: | |
1541 | #print "loading rl:",rlcommand # dbg |
|
1540 | #print "loading rl:",rlcommand # dbg | |
1542 | readline.parse_and_bind(rlcommand) |
|
1541 | readline.parse_and_bind(rlcommand) | |
1543 |
|
1542 | |||
1544 | # Remove some chars from the delimiters list. If we encounter |
|
1543 | # Remove some chars from the delimiters list. If we encounter | |
1545 | # unicode chars, discard them. |
|
1544 | # unicode chars, discard them. | |
1546 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1545 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
1547 | delims = delims.translate(None, self.readline_remove_delims) |
|
1546 | delims = delims.translate(None, self.readline_remove_delims) | |
1548 | delims = delims.replace(ESC_MAGIC, '') |
|
1547 | delims = delims.replace(ESC_MAGIC, '') | |
1549 | readline.set_completer_delims(delims) |
|
1548 | readline.set_completer_delims(delims) | |
1550 | # otherwise we end up with a monster history after a while: |
|
1549 | # otherwise we end up with a monster history after a while: | |
1551 | readline.set_history_length(self.history_length) |
|
1550 | readline.set_history_length(self.history_length) | |
1552 |
|
1551 | |||
1553 | # Load the last 1000 lines from history |
|
1552 | # Load the last 1000 lines from history | |
1554 | for _, _, cell in self.history_manager.get_hist_tail(1000): |
|
1553 | for _, _, cell in self.history_manager.get_hist_tail(1000): | |
1555 | if cell.strip(): # Ignore blank lines |
|
1554 | if cell.strip(): # Ignore blank lines | |
1556 | for line in cell.splitlines(): |
|
1555 | for line in cell.splitlines(): | |
1557 | readline.add_history(line) |
|
1556 | readline.add_history(line) | |
1558 |
|
1557 | |||
1559 | # Configure auto-indent for all platforms |
|
1558 | # Configure auto-indent for all platforms | |
1560 | self.set_autoindent(self.autoindent) |
|
1559 | self.set_autoindent(self.autoindent) | |
1561 |
|
1560 | |||
1562 | def set_next_input(self, s): |
|
1561 | def set_next_input(self, s): | |
1563 | """ Sets the 'default' input string for the next command line. |
|
1562 | """ Sets the 'default' input string for the next command line. | |
1564 |
|
1563 | |||
1565 | Requires readline. |
|
1564 | Requires readline. | |
1566 |
|
1565 | |||
1567 | Example: |
|
1566 | Example: | |
1568 |
|
1567 | |||
1569 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1568 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1570 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1569 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1571 | """ |
|
1570 | """ | |
1572 |
|
1571 | |||
1573 | self.rl_next_input = s |
|
1572 | self.rl_next_input = s | |
1574 |
|
1573 | |||
1575 | # Maybe move this to the terminal subclass? |
|
1574 | # Maybe move this to the terminal subclass? | |
1576 | def pre_readline(self): |
|
1575 | def pre_readline(self): | |
1577 | """readline hook to be used at the start of each line. |
|
1576 | """readline hook to be used at the start of each line. | |
1578 |
|
1577 | |||
1579 | Currently it handles auto-indent only.""" |
|
1578 | Currently it handles auto-indent only.""" | |
1580 |
|
1579 | |||
1581 | if self.rl_do_indent: |
|
1580 | if self.rl_do_indent: | |
1582 | self.readline.insert_text(self._indent_current_str()) |
|
1581 | self.readline.insert_text(self._indent_current_str()) | |
1583 | if self.rl_next_input is not None: |
|
1582 | if self.rl_next_input is not None: | |
1584 | self.readline.insert_text(self.rl_next_input) |
|
1583 | self.readline.insert_text(self.rl_next_input) | |
1585 | self.rl_next_input = None |
|
1584 | self.rl_next_input = None | |
1586 |
|
1585 | |||
1587 | def _indent_current_str(self): |
|
1586 | def _indent_current_str(self): | |
1588 | """return the current level of indentation as a string""" |
|
1587 | """return the current level of indentation as a string""" | |
1589 | return self.input_splitter.indent_spaces * ' ' |
|
1588 | return self.input_splitter.indent_spaces * ' ' | |
1590 |
|
1589 | |||
1591 | #------------------------------------------------------------------------- |
|
1590 | #------------------------------------------------------------------------- | |
1592 | # Things related to text completion |
|
1591 | # Things related to text completion | |
1593 | #------------------------------------------------------------------------- |
|
1592 | #------------------------------------------------------------------------- | |
1594 |
|
1593 | |||
1595 | def init_completer(self): |
|
1594 | def init_completer(self): | |
1596 | """Initialize the completion machinery. |
|
1595 | """Initialize the completion machinery. | |
1597 |
|
1596 | |||
1598 | This creates completion machinery that can be used by client code, |
|
1597 | This creates completion machinery that can be used by client code, | |
1599 | either interactively in-process (typically triggered by the readline |
|
1598 | either interactively in-process (typically triggered by the readline | |
1600 | library), programatically (such as in test suites) or out-of-prcess |
|
1599 | library), programatically (such as in test suites) or out-of-prcess | |
1601 | (typically over the network by remote frontends). |
|
1600 | (typically over the network by remote frontends). | |
1602 | """ |
|
1601 | """ | |
1603 | from IPython.core.completer import IPCompleter |
|
1602 | from IPython.core.completer import IPCompleter | |
1604 | from IPython.core.completerlib import (module_completer, |
|
1603 | from IPython.core.completerlib import (module_completer, | |
1605 | magic_run_completer, cd_completer) |
|
1604 | magic_run_completer, cd_completer) | |
1606 |
|
1605 | |||
1607 | self.Completer = IPCompleter(self, |
|
1606 | self.Completer = IPCompleter(self, | |
1608 | self.user_ns, |
|
1607 | self.user_ns, | |
1609 | self.user_global_ns, |
|
1608 | self.user_global_ns, | |
1610 | self.readline_omit__names, |
|
1609 | self.readline_omit__names, | |
1611 | self.alias_manager.alias_table, |
|
1610 | self.alias_manager.alias_table, | |
1612 | self.has_readline) |
|
1611 | self.has_readline) | |
1613 |
|
1612 | |||
1614 | # Add custom completers to the basic ones built into IPCompleter |
|
1613 | # Add custom completers to the basic ones built into IPCompleter | |
1615 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1614 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1616 | self.strdispatchers['complete_command'] = sdisp |
|
1615 | self.strdispatchers['complete_command'] = sdisp | |
1617 | self.Completer.custom_completers = sdisp |
|
1616 | self.Completer.custom_completers = sdisp | |
1618 |
|
1617 | |||
1619 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1618 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1620 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1619 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1621 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1620 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1622 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1621 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1623 |
|
1622 | |||
1624 | # Only configure readline if we truly are using readline. IPython can |
|
1623 | # Only configure readline if we truly are using readline. IPython can | |
1625 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1624 | # do tab-completion over the network, in GUIs, etc, where readline | |
1626 | # itself may be absent |
|
1625 | # itself may be absent | |
1627 | if self.has_readline: |
|
1626 | if self.has_readline: | |
1628 | self.set_readline_completer() |
|
1627 | self.set_readline_completer() | |
1629 |
|
1628 | |||
1630 | def complete(self, text, line=None, cursor_pos=None): |
|
1629 | def complete(self, text, line=None, cursor_pos=None): | |
1631 | """Return the completed text and a list of completions. |
|
1630 | """Return the completed text and a list of completions. | |
1632 |
|
1631 | |||
1633 | Parameters |
|
1632 | Parameters | |
1634 | ---------- |
|
1633 | ---------- | |
1635 |
|
1634 | |||
1636 | text : string |
|
1635 | text : string | |
1637 | A string of text to be completed on. It can be given as empty and |
|
1636 | A string of text to be completed on. It can be given as empty and | |
1638 | instead a line/position pair are given. In this case, the |
|
1637 | instead a line/position pair are given. In this case, the | |
1639 | completer itself will split the line like readline does. |
|
1638 | completer itself will split the line like readline does. | |
1640 |
|
1639 | |||
1641 | line : string, optional |
|
1640 | line : string, optional | |
1642 | The complete line that text is part of. |
|
1641 | The complete line that text is part of. | |
1643 |
|
1642 | |||
1644 | cursor_pos : int, optional |
|
1643 | cursor_pos : int, optional | |
1645 | The position of the cursor on the input line. |
|
1644 | The position of the cursor on the input line. | |
1646 |
|
1645 | |||
1647 | Returns |
|
1646 | Returns | |
1648 | ------- |
|
1647 | ------- | |
1649 | text : string |
|
1648 | text : string | |
1650 | The actual text that was completed. |
|
1649 | The actual text that was completed. | |
1651 |
|
1650 | |||
1652 | matches : list |
|
1651 | matches : list | |
1653 | A sorted list with all possible completions. |
|
1652 | A sorted list with all possible completions. | |
1654 |
|
1653 | |||
1655 | The optional arguments allow the completion to take more context into |
|
1654 | The optional arguments allow the completion to take more context into | |
1656 | account, and are part of the low-level completion API. |
|
1655 | account, and are part of the low-level completion API. | |
1657 |
|
1656 | |||
1658 | This is a wrapper around the completion mechanism, similar to what |
|
1657 | This is a wrapper around the completion mechanism, similar to what | |
1659 | readline does at the command line when the TAB key is hit. By |
|
1658 | readline does at the command line when the TAB key is hit. By | |
1660 | exposing it as a method, it can be used by other non-readline |
|
1659 | exposing it as a method, it can be used by other non-readline | |
1661 | environments (such as GUIs) for text completion. |
|
1660 | environments (such as GUIs) for text completion. | |
1662 |
|
1661 | |||
1663 | Simple usage example: |
|
1662 | Simple usage example: | |
1664 |
|
1663 | |||
1665 | In [1]: x = 'hello' |
|
1664 | In [1]: x = 'hello' | |
1666 |
|
1665 | |||
1667 | In [2]: _ip.complete('x.l') |
|
1666 | In [2]: _ip.complete('x.l') | |
1668 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1667 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
1669 | """ |
|
1668 | """ | |
1670 |
|
1669 | |||
1671 | # Inject names into __builtin__ so we can complete on the added names. |
|
1670 | # Inject names into __builtin__ so we can complete on the added names. | |
1672 | with self.builtin_trap: |
|
1671 | with self.builtin_trap: | |
1673 | return self.Completer.complete(text, line, cursor_pos) |
|
1672 | return self.Completer.complete(text, line, cursor_pos) | |
1674 |
|
1673 | |||
1675 | def set_custom_completer(self, completer, pos=0): |
|
1674 | def set_custom_completer(self, completer, pos=0): | |
1676 | """Adds a new custom completer function. |
|
1675 | """Adds a new custom completer function. | |
1677 |
|
1676 | |||
1678 | The position argument (defaults to 0) is the index in the completers |
|
1677 | The position argument (defaults to 0) is the index in the completers | |
1679 | list where you want the completer to be inserted.""" |
|
1678 | list where you want the completer to be inserted.""" | |
1680 |
|
1679 | |||
1681 | newcomp = types.MethodType(completer,self.Completer) |
|
1680 | newcomp = types.MethodType(completer,self.Completer) | |
1682 | self.Completer.matchers.insert(pos,newcomp) |
|
1681 | self.Completer.matchers.insert(pos,newcomp) | |
1683 |
|
1682 | |||
1684 | def set_readline_completer(self): |
|
1683 | def set_readline_completer(self): | |
1685 | """Reset readline's completer to be our own.""" |
|
1684 | """Reset readline's completer to be our own.""" | |
1686 | self.readline.set_completer(self.Completer.rlcomplete) |
|
1685 | self.readline.set_completer(self.Completer.rlcomplete) | |
1687 |
|
1686 | |||
1688 | def set_completer_frame(self, frame=None): |
|
1687 | def set_completer_frame(self, frame=None): | |
1689 | """Set the frame of the completer.""" |
|
1688 | """Set the frame of the completer.""" | |
1690 | if frame: |
|
1689 | if frame: | |
1691 | self.Completer.namespace = frame.f_locals |
|
1690 | self.Completer.namespace = frame.f_locals | |
1692 | self.Completer.global_namespace = frame.f_globals |
|
1691 | self.Completer.global_namespace = frame.f_globals | |
1693 | else: |
|
1692 | else: | |
1694 | self.Completer.namespace = self.user_ns |
|
1693 | self.Completer.namespace = self.user_ns | |
1695 | self.Completer.global_namespace = self.user_global_ns |
|
1694 | self.Completer.global_namespace = self.user_global_ns | |
1696 |
|
1695 | |||
1697 | #------------------------------------------------------------------------- |
|
1696 | #------------------------------------------------------------------------- | |
1698 | # Things related to magics |
|
1697 | # Things related to magics | |
1699 | #------------------------------------------------------------------------- |
|
1698 | #------------------------------------------------------------------------- | |
1700 |
|
1699 | |||
1701 | def init_magics(self): |
|
1700 | def init_magics(self): | |
1702 | # FIXME: Move the color initialization to the DisplayHook, which |
|
1701 | # FIXME: Move the color initialization to the DisplayHook, which | |
1703 | # should be split into a prompt manager and displayhook. We probably |
|
1702 | # should be split into a prompt manager and displayhook. We probably | |
1704 | # even need a centralize colors management object. |
|
1703 | # even need a centralize colors management object. | |
1705 | self.magic_colors(self.colors) |
|
1704 | self.magic_colors(self.colors) | |
1706 | # History was moved to a separate module |
|
1705 | # History was moved to a separate module | |
1707 | from . import history |
|
1706 | from . import history | |
1708 | history.init_ipython(self) |
|
1707 | history.init_ipython(self) | |
1709 |
|
1708 | |||
1710 | def magic(self,arg_s): |
|
1709 | def magic(self,arg_s): | |
1711 | """Call a magic function by name. |
|
1710 | """Call a magic function by name. | |
1712 |
|
1711 | |||
1713 | Input: a string containing the name of the magic function to call and |
|
1712 | Input: a string containing the name of the magic function to call and | |
1714 | any additional arguments to be passed to the magic. |
|
1713 | any additional arguments to be passed to the magic. | |
1715 |
|
1714 | |||
1716 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1715 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1717 | prompt: |
|
1716 | prompt: | |
1718 |
|
1717 | |||
1719 | In[1]: %name -opt foo bar |
|
1718 | In[1]: %name -opt foo bar | |
1720 |
|
1719 | |||
1721 | To call a magic without arguments, simply use magic('name'). |
|
1720 | To call a magic without arguments, simply use magic('name'). | |
1722 |
|
1721 | |||
1723 | This provides a proper Python function to call IPython's magics in any |
|
1722 | This provides a proper Python function to call IPython's magics in any | |
1724 | valid Python code you can type at the interpreter, including loops and |
|
1723 | valid Python code you can type at the interpreter, including loops and | |
1725 | compound statements. |
|
1724 | compound statements. | |
1726 | """ |
|
1725 | """ | |
1727 | args = arg_s.split(' ',1) |
|
1726 | args = arg_s.split(' ',1) | |
1728 | magic_name = args[0] |
|
1727 | magic_name = args[0] | |
1729 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1728 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
1730 |
|
1729 | |||
1731 | try: |
|
1730 | try: | |
1732 | magic_args = args[1] |
|
1731 | magic_args = args[1] | |
1733 | except IndexError: |
|
1732 | except IndexError: | |
1734 | magic_args = '' |
|
1733 | magic_args = '' | |
1735 | fn = getattr(self,'magic_'+magic_name,None) |
|
1734 | fn = getattr(self,'magic_'+magic_name,None) | |
1736 | if fn is None: |
|
1735 | if fn is None: | |
1737 | error("Magic function `%s` not found." % magic_name) |
|
1736 | error("Magic function `%s` not found." % magic_name) | |
1738 | else: |
|
1737 | else: | |
1739 | magic_args = self.var_expand(magic_args,1) |
|
1738 | magic_args = self.var_expand(magic_args,1) | |
1740 | with nested(self.builtin_trap,): |
|
1739 | with nested(self.builtin_trap,): | |
1741 | result = fn(magic_args) |
|
1740 | result = fn(magic_args) | |
1742 | return result |
|
1741 | return result | |
1743 |
|
1742 | |||
1744 | def define_magic(self, magicname, func): |
|
1743 | def define_magic(self, magicname, func): | |
1745 | """Expose own function as magic function for ipython |
|
1744 | """Expose own function as magic function for ipython | |
1746 |
|
1745 | |||
1747 | def foo_impl(self,parameter_s=''): |
|
1746 | def foo_impl(self,parameter_s=''): | |
1748 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1747 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1749 | print 'Magic function. Passed parameter is between < >:' |
|
1748 | print 'Magic function. Passed parameter is between < >:' | |
1750 | print '<%s>' % parameter_s |
|
1749 | print '<%s>' % parameter_s | |
1751 | print 'The self object is:',self |
|
1750 | print 'The self object is:',self | |
1752 |
|
1751 | |||
1753 | self.define_magic('foo',foo_impl) |
|
1752 | self.define_magic('foo',foo_impl) | |
1754 | """ |
|
1753 | """ | |
1755 |
|
1754 | |||
1756 | import new |
|
1755 | import new | |
1757 | im = types.MethodType(func,self) |
|
1756 | im = types.MethodType(func,self) | |
1758 | old = getattr(self, "magic_" + magicname, None) |
|
1757 | old = getattr(self, "magic_" + magicname, None) | |
1759 | setattr(self, "magic_" + magicname, im) |
|
1758 | setattr(self, "magic_" + magicname, im) | |
1760 | return old |
|
1759 | return old | |
1761 |
|
1760 | |||
1762 | #------------------------------------------------------------------------- |
|
1761 | #------------------------------------------------------------------------- | |
1763 | # Things related to macros |
|
1762 | # Things related to macros | |
1764 | #------------------------------------------------------------------------- |
|
1763 | #------------------------------------------------------------------------- | |
1765 |
|
1764 | |||
1766 | def define_macro(self, name, themacro): |
|
1765 | def define_macro(self, name, themacro): | |
1767 | """Define a new macro |
|
1766 | """Define a new macro | |
1768 |
|
1767 | |||
1769 | Parameters |
|
1768 | Parameters | |
1770 | ---------- |
|
1769 | ---------- | |
1771 | name : str |
|
1770 | name : str | |
1772 | The name of the macro. |
|
1771 | The name of the macro. | |
1773 | themacro : str or Macro |
|
1772 | themacro : str or Macro | |
1774 | The action to do upon invoking the macro. If a string, a new |
|
1773 | The action to do upon invoking the macro. If a string, a new | |
1775 | Macro object is created by passing the string to it. |
|
1774 | Macro object is created by passing the string to it. | |
1776 | """ |
|
1775 | """ | |
1777 |
|
1776 | |||
1778 | from IPython.core import macro |
|
1777 | from IPython.core import macro | |
1779 |
|
1778 | |||
1780 | if isinstance(themacro, basestring): |
|
1779 | if isinstance(themacro, basestring): | |
1781 | themacro = macro.Macro(themacro) |
|
1780 | themacro = macro.Macro(themacro) | |
1782 | if not isinstance(themacro, macro.Macro): |
|
1781 | if not isinstance(themacro, macro.Macro): | |
1783 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1782 | raise ValueError('A macro must be a string or a Macro instance.') | |
1784 | self.user_ns[name] = themacro |
|
1783 | self.user_ns[name] = themacro | |
1785 |
|
1784 | |||
1786 | #------------------------------------------------------------------------- |
|
1785 | #------------------------------------------------------------------------- | |
1787 | # Things related to the running of system commands |
|
1786 | # Things related to the running of system commands | |
1788 | #------------------------------------------------------------------------- |
|
1787 | #------------------------------------------------------------------------- | |
1789 |
|
1788 | |||
1790 | def system(self, cmd): |
|
1789 | def system(self, cmd): | |
1791 | """Call the given cmd in a subprocess. |
|
1790 | """Call the given cmd in a subprocess. | |
1792 |
|
1791 | |||
1793 | Parameters |
|
1792 | Parameters | |
1794 | ---------- |
|
1793 | ---------- | |
1795 | cmd : str |
|
1794 | cmd : str | |
1796 | Command to execute (can not end in '&', as bacground processes are |
|
1795 | Command to execute (can not end in '&', as bacground processes are | |
1797 | not supported. |
|
1796 | not supported. | |
1798 | """ |
|
1797 | """ | |
1799 | # We do not support backgrounding processes because we either use |
|
1798 | # We do not support backgrounding processes because we either use | |
1800 | # pexpect or pipes to read from. Users can always just call |
|
1799 | # pexpect or pipes to read from. Users can always just call | |
1801 | # os.system() if they really want a background process. |
|
1800 | # os.system() if they really want a background process. | |
1802 | if cmd.endswith('&'): |
|
1801 | if cmd.endswith('&'): | |
1803 | raise OSError("Background processes not supported.") |
|
1802 | raise OSError("Background processes not supported.") | |
1804 |
|
1803 | |||
1805 | return system(self.var_expand(cmd, depth=2)) |
|
1804 | return system(self.var_expand(cmd, depth=2)) | |
1806 |
|
1805 | |||
1807 | def getoutput(self, cmd, split=True): |
|
1806 | def getoutput(self, cmd, split=True): | |
1808 | """Get output (possibly including stderr) from a subprocess. |
|
1807 | """Get output (possibly including stderr) from a subprocess. | |
1809 |
|
1808 | |||
1810 | Parameters |
|
1809 | Parameters | |
1811 | ---------- |
|
1810 | ---------- | |
1812 | cmd : str |
|
1811 | cmd : str | |
1813 | Command to execute (can not end in '&', as background processes are |
|
1812 | Command to execute (can not end in '&', as background processes are | |
1814 | not supported. |
|
1813 | not supported. | |
1815 | split : bool, optional |
|
1814 | split : bool, optional | |
1816 |
|
1815 | |||
1817 | If True, split the output into an IPython SList. Otherwise, an |
|
1816 | If True, split the output into an IPython SList. Otherwise, an | |
1818 | IPython LSString is returned. These are objects similar to normal |
|
1817 | IPython LSString is returned. These are objects similar to normal | |
1819 | lists and strings, with a few convenience attributes for easier |
|
1818 | lists and strings, with a few convenience attributes for easier | |
1820 | manipulation of line-based output. You can use '?' on them for |
|
1819 | manipulation of line-based output. You can use '?' on them for | |
1821 | details. |
|
1820 | details. | |
1822 | """ |
|
1821 | """ | |
1823 | if cmd.endswith('&'): |
|
1822 | if cmd.endswith('&'): | |
1824 | raise OSError("Background processes not supported.") |
|
1823 | raise OSError("Background processes not supported.") | |
1825 | out = getoutput(self.var_expand(cmd, depth=2)) |
|
1824 | out = getoutput(self.var_expand(cmd, depth=2)) | |
1826 | if split: |
|
1825 | if split: | |
1827 | out = SList(out.splitlines()) |
|
1826 | out = SList(out.splitlines()) | |
1828 | else: |
|
1827 | else: | |
1829 | out = LSString(out) |
|
1828 | out = LSString(out) | |
1830 | return out |
|
1829 | return out | |
1831 |
|
1830 | |||
1832 | #------------------------------------------------------------------------- |
|
1831 | #------------------------------------------------------------------------- | |
1833 | # Things related to aliases |
|
1832 | # Things related to aliases | |
1834 | #------------------------------------------------------------------------- |
|
1833 | #------------------------------------------------------------------------- | |
1835 |
|
1834 | |||
1836 | def init_alias(self): |
|
1835 | def init_alias(self): | |
1837 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
1836 | self.alias_manager = AliasManager(shell=self, config=self.config) | |
1838 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
1837 | self.ns_table['alias'] = self.alias_manager.alias_table, | |
1839 |
|
1838 | |||
1840 | #------------------------------------------------------------------------- |
|
1839 | #------------------------------------------------------------------------- | |
1841 | # Things related to extensions and plugins |
|
1840 | # Things related to extensions and plugins | |
1842 | #------------------------------------------------------------------------- |
|
1841 | #------------------------------------------------------------------------- | |
1843 |
|
1842 | |||
1844 | def init_extension_manager(self): |
|
1843 | def init_extension_manager(self): | |
1845 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
1844 | self.extension_manager = ExtensionManager(shell=self, config=self.config) | |
1846 |
|
1845 | |||
1847 | def init_plugin_manager(self): |
|
1846 | def init_plugin_manager(self): | |
1848 | self.plugin_manager = PluginManager(config=self.config) |
|
1847 | self.plugin_manager = PluginManager(config=self.config) | |
1849 |
|
1848 | |||
1850 | #------------------------------------------------------------------------- |
|
1849 | #------------------------------------------------------------------------- | |
1851 | # Things related to payloads |
|
1850 | # Things related to payloads | |
1852 | #------------------------------------------------------------------------- |
|
1851 | #------------------------------------------------------------------------- | |
1853 |
|
1852 | |||
1854 | def init_payload(self): |
|
1853 | def init_payload(self): | |
1855 | self.payload_manager = PayloadManager(config=self.config) |
|
1854 | self.payload_manager = PayloadManager(config=self.config) | |
1856 |
|
1855 | |||
1857 | #------------------------------------------------------------------------- |
|
1856 | #------------------------------------------------------------------------- | |
1858 | # Things related to the prefilter |
|
1857 | # Things related to the prefilter | |
1859 | #------------------------------------------------------------------------- |
|
1858 | #------------------------------------------------------------------------- | |
1860 |
|
1859 | |||
1861 | def init_prefilter(self): |
|
1860 | def init_prefilter(self): | |
1862 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
1861 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) | |
1863 | # Ultimately this will be refactored in the new interpreter code, but |
|
1862 | # Ultimately this will be refactored in the new interpreter code, but | |
1864 | # for now, we should expose the main prefilter method (there's legacy |
|
1863 | # for now, we should expose the main prefilter method (there's legacy | |
1865 | # code out there that may rely on this). |
|
1864 | # code out there that may rely on this). | |
1866 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
1865 | self.prefilter = self.prefilter_manager.prefilter_lines | |
1867 |
|
1866 | |||
1868 | def auto_rewrite_input(self, cmd): |
|
1867 | def auto_rewrite_input(self, cmd): | |
1869 | """Print to the screen the rewritten form of the user's command. |
|
1868 | """Print to the screen the rewritten form of the user's command. | |
1870 |
|
1869 | |||
1871 | This shows visual feedback by rewriting input lines that cause |
|
1870 | This shows visual feedback by rewriting input lines that cause | |
1872 | automatic calling to kick in, like:: |
|
1871 | automatic calling to kick in, like:: | |
1873 |
|
1872 | |||
1874 | /f x |
|
1873 | /f x | |
1875 |
|
1874 | |||
1876 | into:: |
|
1875 | into:: | |
1877 |
|
1876 | |||
1878 | ------> f(x) |
|
1877 | ------> f(x) | |
1879 |
|
1878 | |||
1880 | after the user's input prompt. This helps the user understand that the |
|
1879 | after the user's input prompt. This helps the user understand that the | |
1881 | input line was transformed automatically by IPython. |
|
1880 | input line was transformed automatically by IPython. | |
1882 | """ |
|
1881 | """ | |
1883 | rw = self.displayhook.prompt1.auto_rewrite() + cmd |
|
1882 | rw = self.displayhook.prompt1.auto_rewrite() + cmd | |
1884 |
|
1883 | |||
1885 | try: |
|
1884 | try: | |
1886 | # plain ascii works better w/ pyreadline, on some machines, so |
|
1885 | # plain ascii works better w/ pyreadline, on some machines, so | |
1887 | # we use it and only print uncolored rewrite if we have unicode |
|
1886 | # we use it and only print uncolored rewrite if we have unicode | |
1888 | rw = str(rw) |
|
1887 | rw = str(rw) | |
1889 | print >> IPython.utils.io.Term.cout, rw |
|
1888 | print >> IPython.utils.io.Term.cout, rw | |
1890 | except UnicodeEncodeError: |
|
1889 | except UnicodeEncodeError: | |
1891 | print "------> " + cmd |
|
1890 | print "------> " + cmd | |
1892 |
|
1891 | |||
1893 | #------------------------------------------------------------------------- |
|
1892 | #------------------------------------------------------------------------- | |
1894 | # Things related to extracting values/expressions from kernel and user_ns |
|
1893 | # Things related to extracting values/expressions from kernel and user_ns | |
1895 | #------------------------------------------------------------------------- |
|
1894 | #------------------------------------------------------------------------- | |
1896 |
|
1895 | |||
1897 | def _simple_error(self): |
|
1896 | def _simple_error(self): | |
1898 | etype, value = sys.exc_info()[:2] |
|
1897 | etype, value = sys.exc_info()[:2] | |
1899 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) |
|
1898 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) | |
1900 |
|
1899 | |||
1901 | def user_variables(self, names): |
|
1900 | def user_variables(self, names): | |
1902 | """Get a list of variable names from the user's namespace. |
|
1901 | """Get a list of variable names from the user's namespace. | |
1903 |
|
1902 | |||
1904 | Parameters |
|
1903 | Parameters | |
1905 | ---------- |
|
1904 | ---------- | |
1906 | names : list of strings |
|
1905 | names : list of strings | |
1907 | A list of names of variables to be read from the user namespace. |
|
1906 | A list of names of variables to be read from the user namespace. | |
1908 |
|
1907 | |||
1909 | Returns |
|
1908 | Returns | |
1910 | ------- |
|
1909 | ------- | |
1911 | A dict, keyed by the input names and with the repr() of each value. |
|
1910 | A dict, keyed by the input names and with the repr() of each value. | |
1912 | """ |
|
1911 | """ | |
1913 | out = {} |
|
1912 | out = {} | |
1914 | user_ns = self.user_ns |
|
1913 | user_ns = self.user_ns | |
1915 | for varname in names: |
|
1914 | for varname in names: | |
1916 | try: |
|
1915 | try: | |
1917 | value = repr(user_ns[varname]) |
|
1916 | value = repr(user_ns[varname]) | |
1918 | except: |
|
1917 | except: | |
1919 | value = self._simple_error() |
|
1918 | value = self._simple_error() | |
1920 | out[varname] = value |
|
1919 | out[varname] = value | |
1921 | return out |
|
1920 | return out | |
1922 |
|
1921 | |||
1923 | def user_expressions(self, expressions): |
|
1922 | def user_expressions(self, expressions): | |
1924 | """Evaluate a dict of expressions in the user's namespace. |
|
1923 | """Evaluate a dict of expressions in the user's namespace. | |
1925 |
|
1924 | |||
1926 | Parameters |
|
1925 | Parameters | |
1927 | ---------- |
|
1926 | ---------- | |
1928 | expressions : dict |
|
1927 | expressions : dict | |
1929 | A dict with string keys and string values. The expression values |
|
1928 | A dict with string keys and string values. The expression values | |
1930 | should be valid Python expressions, each of which will be evaluated |
|
1929 | should be valid Python expressions, each of which will be evaluated | |
1931 | in the user namespace. |
|
1930 | in the user namespace. | |
1932 |
|
1931 | |||
1933 | Returns |
|
1932 | Returns | |
1934 | ------- |
|
1933 | ------- | |
1935 | A dict, keyed like the input expressions dict, with the repr() of each |
|
1934 | A dict, keyed like the input expressions dict, with the repr() of each | |
1936 | value. |
|
1935 | value. | |
1937 | """ |
|
1936 | """ | |
1938 | out = {} |
|
1937 | out = {} | |
1939 | user_ns = self.user_ns |
|
1938 | user_ns = self.user_ns | |
1940 | global_ns = self.user_global_ns |
|
1939 | global_ns = self.user_global_ns | |
1941 | for key, expr in expressions.iteritems(): |
|
1940 | for key, expr in expressions.iteritems(): | |
1942 | try: |
|
1941 | try: | |
1943 | value = repr(eval(expr, global_ns, user_ns)) |
|
1942 | value = repr(eval(expr, global_ns, user_ns)) | |
1944 | except: |
|
1943 | except: | |
1945 | value = self._simple_error() |
|
1944 | value = self._simple_error() | |
1946 | out[key] = value |
|
1945 | out[key] = value | |
1947 | return out |
|
1946 | return out | |
1948 |
|
1947 | |||
1949 | #------------------------------------------------------------------------- |
|
1948 | #------------------------------------------------------------------------- | |
1950 | # Things related to the running of code |
|
1949 | # Things related to the running of code | |
1951 | #------------------------------------------------------------------------- |
|
1950 | #------------------------------------------------------------------------- | |
1952 |
|
1951 | |||
1953 | def ex(self, cmd): |
|
1952 | def ex(self, cmd): | |
1954 | """Execute a normal python statement in user namespace.""" |
|
1953 | """Execute a normal python statement in user namespace.""" | |
1955 | with nested(self.builtin_trap,): |
|
1954 | with nested(self.builtin_trap,): | |
1956 | exec cmd in self.user_global_ns, self.user_ns |
|
1955 | exec cmd in self.user_global_ns, self.user_ns | |
1957 |
|
1956 | |||
1958 | def ev(self, expr): |
|
1957 | def ev(self, expr): | |
1959 | """Evaluate python expression expr in user namespace. |
|
1958 | """Evaluate python expression expr in user namespace. | |
1960 |
|
1959 | |||
1961 | Returns the result of evaluation |
|
1960 | Returns the result of evaluation | |
1962 | """ |
|
1961 | """ | |
1963 | with nested(self.builtin_trap,): |
|
1962 | with nested(self.builtin_trap,): | |
1964 | return eval(expr, self.user_global_ns, self.user_ns) |
|
1963 | return eval(expr, self.user_global_ns, self.user_ns) | |
1965 |
|
1964 | |||
1966 | def safe_execfile(self, fname, *where, **kw): |
|
1965 | def safe_execfile(self, fname, *where, **kw): | |
1967 | """A safe version of the builtin execfile(). |
|
1966 | """A safe version of the builtin execfile(). | |
1968 |
|
1967 | |||
1969 | This version will never throw an exception, but instead print |
|
1968 | This version will never throw an exception, but instead print | |
1970 | helpful error messages to the screen. This only works on pure |
|
1969 | helpful error messages to the screen. This only works on pure | |
1971 | Python files with the .py extension. |
|
1970 | Python files with the .py extension. | |
1972 |
|
1971 | |||
1973 | Parameters |
|
1972 | Parameters | |
1974 | ---------- |
|
1973 | ---------- | |
1975 | fname : string |
|
1974 | fname : string | |
1976 | The name of the file to be executed. |
|
1975 | The name of the file to be executed. | |
1977 | where : tuple |
|
1976 | where : tuple | |
1978 | One or two namespaces, passed to execfile() as (globals,locals). |
|
1977 | One or two namespaces, passed to execfile() as (globals,locals). | |
1979 | If only one is given, it is passed as both. |
|
1978 | If only one is given, it is passed as both. | |
1980 | exit_ignore : bool (False) |
|
1979 | exit_ignore : bool (False) | |
1981 | If True, then silence SystemExit for non-zero status (it is always |
|
1980 | If True, then silence SystemExit for non-zero status (it is always | |
1982 | silenced for zero status, as it is so common). |
|
1981 | silenced for zero status, as it is so common). | |
1983 | """ |
|
1982 | """ | |
1984 | kw.setdefault('exit_ignore', False) |
|
1983 | kw.setdefault('exit_ignore', False) | |
1985 |
|
1984 | |||
1986 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
1985 | fname = os.path.abspath(os.path.expanduser(fname)) | |
1987 |
|
1986 | |||
1988 | # Make sure we have a .py file |
|
1987 | # Make sure we have a .py file | |
1989 | if not fname.endswith('.py'): |
|
1988 | if not fname.endswith('.py'): | |
1990 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
1989 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
1991 |
|
1990 | |||
1992 | # Make sure we can open the file |
|
1991 | # Make sure we can open the file | |
1993 | try: |
|
1992 | try: | |
1994 | with open(fname) as thefile: |
|
1993 | with open(fname) as thefile: | |
1995 | pass |
|
1994 | pass | |
1996 | except: |
|
1995 | except: | |
1997 | warn('Could not open file <%s> for safe execution.' % fname) |
|
1996 | warn('Could not open file <%s> for safe execution.' % fname) | |
1998 | return |
|
1997 | return | |
1999 |
|
1998 | |||
2000 | # Find things also in current directory. This is needed to mimic the |
|
1999 | # Find things also in current directory. This is needed to mimic the | |
2001 | # behavior of running a script from the system command line, where |
|
2000 | # behavior of running a script from the system command line, where | |
2002 | # Python inserts the script's directory into sys.path |
|
2001 | # Python inserts the script's directory into sys.path | |
2003 | dname = os.path.dirname(fname) |
|
2002 | dname = os.path.dirname(fname) | |
2004 |
|
2003 | |||
2005 | with prepended_to_syspath(dname): |
|
2004 | with prepended_to_syspath(dname): | |
2006 | try: |
|
2005 | try: | |
2007 | execfile(fname,*where) |
|
2006 | execfile(fname,*where) | |
2008 | except SystemExit, status: |
|
2007 | except SystemExit, status: | |
2009 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2008 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2010 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2009 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2011 | # these are considered normal by the OS: |
|
2010 | # these are considered normal by the OS: | |
2012 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2011 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2013 | # 0 |
|
2012 | # 0 | |
2014 | # > python -c'import sys;sys.exit()'; echo $? |
|
2013 | # > python -c'import sys;sys.exit()'; echo $? | |
2015 | # 0 |
|
2014 | # 0 | |
2016 | # For other exit status, we show the exception unless |
|
2015 | # For other exit status, we show the exception unless | |
2017 | # explicitly silenced, but only in short form. |
|
2016 | # explicitly silenced, but only in short form. | |
2018 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
2017 | if status.code not in (0, None) and not kw['exit_ignore']: | |
2019 | self.showtraceback(exception_only=True) |
|
2018 | self.showtraceback(exception_only=True) | |
2020 | except: |
|
2019 | except: | |
2021 | self.showtraceback() |
|
2020 | self.showtraceback() | |
2022 |
|
2021 | |||
2023 | def safe_execfile_ipy(self, fname): |
|
2022 | def safe_execfile_ipy(self, fname): | |
2024 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2023 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
2025 |
|
2024 | |||
2026 | Parameters |
|
2025 | Parameters | |
2027 | ---------- |
|
2026 | ---------- | |
2028 | fname : str |
|
2027 | fname : str | |
2029 | The name of the file to execute. The filename must have a |
|
2028 | The name of the file to execute. The filename must have a | |
2030 | .ipy extension. |
|
2029 | .ipy extension. | |
2031 | """ |
|
2030 | """ | |
2032 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2031 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2033 |
|
2032 | |||
2034 | # Make sure we have a .py file |
|
2033 | # Make sure we have a .py file | |
2035 | if not fname.endswith('.ipy'): |
|
2034 | if not fname.endswith('.ipy'): | |
2036 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
2035 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
2037 |
|
2036 | |||
2038 | # Make sure we can open the file |
|
2037 | # Make sure we can open the file | |
2039 | try: |
|
2038 | try: | |
2040 | with open(fname) as thefile: |
|
2039 | with open(fname) as thefile: | |
2041 | pass |
|
2040 | pass | |
2042 | except: |
|
2041 | except: | |
2043 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2042 | warn('Could not open file <%s> for safe execution.' % fname) | |
2044 | return |
|
2043 | return | |
2045 |
|
2044 | |||
2046 | # Find things also in current directory. This is needed to mimic the |
|
2045 | # Find things also in current directory. This is needed to mimic the | |
2047 | # behavior of running a script from the system command line, where |
|
2046 | # behavior of running a script from the system command line, where | |
2048 | # Python inserts the script's directory into sys.path |
|
2047 | # Python inserts the script's directory into sys.path | |
2049 | dname = os.path.dirname(fname) |
|
2048 | dname = os.path.dirname(fname) | |
2050 |
|
2049 | |||
2051 | with prepended_to_syspath(dname): |
|
2050 | with prepended_to_syspath(dname): | |
2052 | try: |
|
2051 | try: | |
2053 | with open(fname) as thefile: |
|
2052 | with open(fname) as thefile: | |
2054 | # self.run_cell currently captures all exceptions |
|
2053 | # self.run_cell currently captures all exceptions | |
2055 | # raised in user code. It would be nice if there were |
|
2054 | # raised in user code. It would be nice if there were | |
2056 | # versions of runlines, execfile that did raise, so |
|
2055 | # versions of runlines, execfile that did raise, so | |
2057 | # we could catch the errors. |
|
2056 | # we could catch the errors. | |
2058 | self.run_cell(thefile.read()) |
|
2057 | self.run_cell(thefile.read()) | |
2059 | except: |
|
2058 | except: | |
2060 | self.showtraceback() |
|
2059 | self.showtraceback() | |
2061 | warn('Unknown failure executing file: <%s>' % fname) |
|
2060 | warn('Unknown failure executing file: <%s>' % fname) | |
2062 |
|
2061 | |||
2063 | def run_cell(self, cell): |
|
2062 | def run_cell(self, cell): | |
2064 | """Run the contents of an entire multiline 'cell' of code. |
|
2063 | """Run the contents of an entire multiline 'cell' of code. | |
2065 |
|
2064 | |||
2066 | The cell is split into separate blocks which can be executed |
|
2065 | The cell is split into separate blocks which can be executed | |
2067 | individually. Then, based on how many blocks there are, they are |
|
2066 | individually. Then, based on how many blocks there are, they are | |
2068 | executed as follows: |
|
2067 | executed as follows: | |
2069 |
|
2068 | |||
2070 | - A single block: 'single' mode. |
|
2069 | - A single block: 'single' mode. | |
2071 |
|
2070 | |||
2072 | If there's more than one block, it depends: |
|
2071 | If there's more than one block, it depends: | |
2073 |
|
2072 | |||
2074 | - if the last one is no more than two lines long, run all but the last |
|
2073 | - if the last one is no more than two lines long, run all but the last | |
2075 | in 'exec' mode and the very last one in 'single' mode. This makes it |
|
2074 | in 'exec' mode and the very last one in 'single' mode. This makes it | |
2076 | easy to type simple expressions at the end to see computed values. - |
|
2075 | easy to type simple expressions at the end to see computed values. - | |
2077 | otherwise (last one is also multiline), run all in 'exec' mode |
|
2076 | otherwise (last one is also multiline), run all in 'exec' mode | |
2078 |
|
2077 | |||
2079 | When code is executed in 'single' mode, :func:`sys.displayhook` fires, |
|
2078 | When code is executed in 'single' mode, :func:`sys.displayhook` fires, | |
2080 | results are displayed and output prompts are computed. In 'exec' mode, |
|
2079 | results are displayed and output prompts are computed. In 'exec' mode, | |
2081 | no results are displayed unless :func:`print` is called explicitly; |
|
2080 | no results are displayed unless :func:`print` is called explicitly; | |
2082 | this mode is more akin to running a script. |
|
2081 | this mode is more akin to running a script. | |
2083 |
|
2082 | |||
2084 | Parameters |
|
2083 | Parameters | |
2085 | ---------- |
|
2084 | ---------- | |
2086 | cell : str |
|
2085 | cell : str | |
2087 | A single or multiline string. |
|
2086 | A single or multiline string. | |
2088 | """ |
|
2087 | """ | |
2089 |
|
2088 | |||
2090 | # We need to break up the input into executable blocks that can be run |
|
2089 | # We need to break up the input into executable blocks that can be run | |
2091 | # in 'single' mode, to provide comfortable user behavior. |
|
2090 | # in 'single' mode, to provide comfortable user behavior. | |
2092 | blocks = self.input_splitter.split_blocks(cell) |
|
2091 | blocks = self.input_splitter.split_blocks(cell) | |
2093 |
|
2092 | |||
2094 | if not blocks: |
|
2093 | if not blocks: | |
2095 | return |
|
2094 | return | |
2096 |
|
2095 | |||
2097 | # Store the 'ipython' version of the cell as well, since that's what |
|
2096 | # Store the 'ipython' version of the cell as well, since that's what | |
2098 | # needs to go into the translated history and get executed (the |
|
2097 | # needs to go into the translated history and get executed (the | |
2099 | # original cell may contain non-python syntax). |
|
2098 | # original cell may contain non-python syntax). | |
2100 | ipy_cell = ''.join(blocks) |
|
2099 | ipy_cell = ''.join(blocks) | |
2101 |
|
2100 | |||
2102 | # Store raw and processed history |
|
2101 | # Store raw and processed history | |
2103 | self.history_manager.store_inputs(self.execution_count, ipy_cell, cell) |
|
2102 | self.history_manager.store_inputs(self.execution_count, ipy_cell, cell) | |
2104 |
|
2103 | |||
2105 | self.logger.log(ipy_cell, cell) |
|
2104 | self.logger.log(ipy_cell, cell) | |
2106 |
|
2105 | |||
2107 | # All user code execution must happen with our context managers active |
|
2106 | # All user code execution must happen with our context managers active | |
2108 | with nested(self.builtin_trap, self.display_trap): |
|
2107 | with nested(self.builtin_trap, self.display_trap): | |
2109 |
|
2108 | |||
2110 | # Single-block input should behave like an interactive prompt |
|
2109 | # Single-block input should behave like an interactive prompt | |
2111 | if len(blocks) == 1: |
|
2110 | if len(blocks) == 1: | |
2112 | # since we return here, we need to update the execution count |
|
2111 | # since we return here, we need to update the execution count | |
2113 | out = self.run_one_block(blocks[0]) |
|
2112 | out = self.run_one_block(blocks[0]) | |
2114 | self.execution_count += 1 |
|
2113 | self.execution_count += 1 | |
2115 | return out |
|
2114 | return out | |
2116 |
|
2115 | |||
2117 | # In multi-block input, if the last block is a simple (one-two |
|
2116 | # In multi-block input, if the last block is a simple (one-two | |
2118 | # lines) expression, run it in single mode so it produces output. |
|
2117 | # lines) expression, run it in single mode so it produces output. | |
2119 | # Otherwise just feed the whole thing to run_code. This seems like |
|
2118 | # Otherwise just feed the whole thing to run_code. This seems like | |
2120 | # a reasonable usability design. |
|
2119 | # a reasonable usability design. | |
2121 | last = blocks[-1] |
|
2120 | last = blocks[-1] | |
2122 | last_nlines = len(last.splitlines()) |
|
2121 | last_nlines = len(last.splitlines()) | |
2123 |
|
2122 | |||
2124 | # Note: below, whenever we call run_code, we must sync history |
|
2123 | # Note: below, whenever we call run_code, we must sync history | |
2125 | # ourselves, because run_code is NOT meant to manage history at all. |
|
2124 | # ourselves, because run_code is NOT meant to manage history at all. | |
2126 | if last_nlines < 2: |
|
2125 | if last_nlines < 2: | |
2127 | # Here we consider the cell split between 'body' and 'last', |
|
2126 | # Here we consider the cell split between 'body' and 'last', | |
2128 | # store all history and execute 'body', and if successful, then |
|
2127 | # store all history and execute 'body', and if successful, then | |
2129 | # proceed to execute 'last'. |
|
2128 | # proceed to execute 'last'. | |
2130 |
|
2129 | |||
2131 | # Get the main body to run as a cell |
|
2130 | # Get the main body to run as a cell | |
2132 | ipy_body = ''.join(blocks[:-1]) |
|
2131 | ipy_body = ''.join(blocks[:-1]) | |
2133 | retcode = self.run_source(ipy_body, symbol='exec', |
|
2132 | retcode = self.run_source(ipy_body, symbol='exec', | |
2134 | post_execute=False) |
|
2133 | post_execute=False) | |
2135 | if retcode==0: |
|
2134 | if retcode==0: | |
2136 | # And the last expression via runlines so it produces output |
|
2135 | # And the last expression via runlines so it produces output | |
2137 | self.run_one_block(last) |
|
2136 | self.run_one_block(last) | |
2138 | else: |
|
2137 | else: | |
2139 | # Run the whole cell as one entity, storing both raw and |
|
2138 | # Run the whole cell as one entity, storing both raw and | |
2140 | # processed input in history |
|
2139 | # processed input in history | |
2141 | self.run_source(ipy_cell, symbol='exec') |
|
2140 | self.run_source(ipy_cell, symbol='exec') | |
2142 |
|
2141 | |||
2143 | # Each cell is a *single* input, regardless of how many lines it has |
|
2142 | # Each cell is a *single* input, regardless of how many lines it has | |
2144 | self.execution_count += 1 |
|
2143 | self.execution_count += 1 | |
2145 |
|
2144 | |||
2146 | def run_one_block(self, block): |
|
2145 | def run_one_block(self, block): | |
2147 | """Run a single interactive block of source code. |
|
2146 | """Run a single interactive block of source code. | |
2148 |
|
2147 | |||
2149 | If the block is single-line, dynamic transformations are applied to it |
|
2148 | If the block is single-line, dynamic transformations are applied to it | |
2150 | (like automagics, autocall and alias recognition). |
|
2149 | (like automagics, autocall and alias recognition). | |
2151 |
|
2150 | |||
2152 | If the block is multi-line, it must consist of valid Python code only. |
|
2151 | If the block is multi-line, it must consist of valid Python code only. | |
2153 |
|
2152 | |||
2154 | Parameters |
|
2153 | Parameters | |
2155 | ---------- |
|
2154 | ---------- | |
2156 | block : string |
|
2155 | block : string | |
2157 | A (possibly multiline) string of code to be executed. |
|
2156 | A (possibly multiline) string of code to be executed. | |
2158 |
|
2157 | |||
2159 | Returns |
|
2158 | Returns | |
2160 | ------- |
|
2159 | ------- | |
2161 | The output of the underlying execution method used, be it |
|
2160 | The output of the underlying execution method used, be it | |
2162 | :meth:`run_source` or :meth:`run_single_line`. |
|
2161 | :meth:`run_source` or :meth:`run_single_line`. | |
2163 | """ |
|
2162 | """ | |
2164 | if len(block.splitlines()) <= 1: |
|
2163 | if len(block.splitlines()) <= 1: | |
2165 | out = self.run_single_line(block) |
|
2164 | out = self.run_single_line(block) | |
2166 | else: |
|
2165 | else: | |
2167 | # Call run_source, which correctly compiles the input cell. |
|
2166 | # Call run_source, which correctly compiles the input cell. | |
2168 | # run_code must only be called when we know we have a code object, |
|
2167 | # run_code must only be called when we know we have a code object, | |
2169 | # as it does a naked exec and the compilation mode may not be what |
|
2168 | # as it does a naked exec and the compilation mode may not be what | |
2170 | # we wanted. |
|
2169 | # we wanted. | |
2171 | out = self.run_source(block) |
|
2170 | out = self.run_source(block) | |
2172 | return out |
|
2171 | return out | |
2173 |
|
2172 | |||
2174 | def run_single_line(self, line): |
|
2173 | def run_single_line(self, line): | |
2175 | """Run a single-line interactive statement. |
|
2174 | """Run a single-line interactive statement. | |
2176 |
|
2175 | |||
2177 | This assumes the input has been transformed to IPython syntax by |
|
2176 | This assumes the input has been transformed to IPython syntax by | |
2178 | applying all static transformations (those with an explicit prefix like |
|
2177 | applying all static transformations (those with an explicit prefix like | |
2179 | % or !), but it will further try to apply the dynamic ones. |
|
2178 | % or !), but it will further try to apply the dynamic ones. | |
2180 |
|
2179 | |||
2181 | It does not update history. |
|
2180 | It does not update history. | |
2182 | """ |
|
2181 | """ | |
2183 | tline = self.prefilter_manager.prefilter_line(line) |
|
2182 | tline = self.prefilter_manager.prefilter_line(line) | |
2184 | return self.run_source(tline) |
|
2183 | return self.run_source(tline) | |
2185 |
|
2184 | |||
2186 | # PENDING REMOVAL: this method is slated for deletion, once our new |
|
2185 | # PENDING REMOVAL: this method is slated for deletion, once our new | |
2187 | # input logic has been 100% moved to frontends and is stable. |
|
2186 | # input logic has been 100% moved to frontends and is stable. | |
2188 | def runlines(self, lines, clean=False): |
|
2187 | def runlines(self, lines, clean=False): | |
2189 | """Run a string of one or more lines of source. |
|
2188 | """Run a string of one or more lines of source. | |
2190 |
|
2189 | |||
2191 | This method is capable of running a string containing multiple source |
|
2190 | This method is capable of running a string containing multiple source | |
2192 | lines, as if they had been entered at the IPython prompt. Since it |
|
2191 | lines, as if they had been entered at the IPython prompt. Since it | |
2193 | exposes IPython's processing machinery, the given strings can contain |
|
2192 | exposes IPython's processing machinery, the given strings can contain | |
2194 | magic calls (%magic), special shell access (!cmd), etc. |
|
2193 | magic calls (%magic), special shell access (!cmd), etc. | |
2195 | """ |
|
2194 | """ | |
2196 |
|
2195 | |||
2197 | if isinstance(lines, (list, tuple)): |
|
2196 | if not isinstance(lines, (list, tuple)): | |
2198 |
lines = |
|
2197 | lines = lines.splitlines() | |
2199 |
|
2198 | |||
2200 | if clean: |
|
2199 | if clean: | |
2201 | lines = self._cleanup_ipy_script(lines) |
|
2200 | lines = self._cleanup_ipy_script(lines) | |
2202 |
|
2201 | |||
2203 | # We must start with a clean buffer, in case this is run from an |
|
2202 | # We must start with a clean buffer, in case this is run from an | |
2204 | # interactive IPython session (via a magic, for example). |
|
2203 | # interactive IPython session (via a magic, for example). | |
2205 | self.reset_buffer() |
|
2204 | self.reset_buffer() | |
2206 | lines = lines.splitlines() |
|
|||
2207 |
|
2205 | |||
2208 | # Since we will prefilter all lines, store the user's raw input too |
|
2206 | # Since we will prefilter all lines, store the user's raw input too | |
2209 | # before we apply any transformations |
|
2207 | # before we apply any transformations | |
2210 | self.buffer_raw[:] = [ l+'\n' for l in lines] |
|
2208 | self.buffer_raw[:] = [ l+'\n' for l in lines] | |
2211 |
|
2209 | |||
2212 | more = False |
|
2210 | more = False | |
2213 | prefilter_lines = self.prefilter_manager.prefilter_lines |
|
2211 | prefilter_lines = self.prefilter_manager.prefilter_lines | |
2214 | with nested(self.builtin_trap, self.display_trap): |
|
2212 | with nested(self.builtin_trap, self.display_trap): | |
2215 | for line in lines: |
|
2213 | for line in lines: | |
2216 | # skip blank lines so we don't mess up the prompt counter, but |
|
2214 | # skip blank lines so we don't mess up the prompt counter, but | |
2217 | # do NOT skip even a blank line if we are in a code block (more |
|
2215 | # do NOT skip even a blank line if we are in a code block (more | |
2218 | # is true) |
|
2216 | # is true) | |
2219 |
|
2217 | |||
2220 | if line or more: |
|
2218 | if line or more: | |
2221 | more = self.push_line(prefilter_lines(line, more)) |
|
2219 | more = self.push_line(prefilter_lines(line, more)) | |
2222 | # IPython's run_source returns None if there was an error |
|
2220 | # IPython's run_source returns None if there was an error | |
2223 | # compiling the code. This allows us to stop processing |
|
2221 | # compiling the code. This allows us to stop processing | |
2224 | # right away, so the user gets the error message at the |
|
2222 | # right away, so the user gets the error message at the | |
2225 | # right place. |
|
2223 | # right place. | |
2226 | if more is None: |
|
2224 | if more is None: | |
2227 | break |
|
2225 | break | |
2228 | # final newline in case the input didn't have it, so that the code |
|
2226 | # final newline in case the input didn't have it, so that the code | |
2229 | # actually does get executed |
|
2227 | # actually does get executed | |
2230 | if more: |
|
2228 | if more: | |
2231 | self.push_line('\n') |
|
2229 | self.push_line('\n') | |
2232 |
|
2230 | |||
2233 | def run_source(self, source, filename=None, |
|
2231 | def run_source(self, source, filename=None, | |
2234 | symbol='single', post_execute=True): |
|
2232 | symbol='single', post_execute=True): | |
2235 | """Compile and run some source in the interpreter. |
|
2233 | """Compile and run some source in the interpreter. | |
2236 |
|
2234 | |||
2237 | Arguments are as for compile_command(). |
|
2235 | Arguments are as for compile_command(). | |
2238 |
|
2236 | |||
2239 | One several things can happen: |
|
2237 | One several things can happen: | |
2240 |
|
2238 | |||
2241 | 1) The input is incorrect; compile_command() raised an |
|
2239 | 1) The input is incorrect; compile_command() raised an | |
2242 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2240 | exception (SyntaxError or OverflowError). A syntax traceback | |
2243 | will be printed by calling the showsyntaxerror() method. |
|
2241 | will be printed by calling the showsyntaxerror() method. | |
2244 |
|
2242 | |||
2245 | 2) The input is incomplete, and more input is required; |
|
2243 | 2) The input is incomplete, and more input is required; | |
2246 | compile_command() returned None. Nothing happens. |
|
2244 | compile_command() returned None. Nothing happens. | |
2247 |
|
2245 | |||
2248 | 3) The input is complete; compile_command() returned a code |
|
2246 | 3) The input is complete; compile_command() returned a code | |
2249 | object. The code is executed by calling self.run_code() (which |
|
2247 | object. The code is executed by calling self.run_code() (which | |
2250 | also handles run-time exceptions, except for SystemExit). |
|
2248 | also handles run-time exceptions, except for SystemExit). | |
2251 |
|
2249 | |||
2252 | The return value is: |
|
2250 | The return value is: | |
2253 |
|
2251 | |||
2254 | - True in case 2 |
|
2252 | - True in case 2 | |
2255 |
|
2253 | |||
2256 | - False in the other cases, unless an exception is raised, where |
|
2254 | - False in the other cases, unless an exception is raised, where | |
2257 | None is returned instead. This can be used by external callers to |
|
2255 | None is returned instead. This can be used by external callers to | |
2258 | know whether to continue feeding input or not. |
|
2256 | know whether to continue feeding input or not. | |
2259 |
|
2257 | |||
2260 | The return value can be used to decide whether to use sys.ps1 or |
|
2258 | The return value can be used to decide whether to use sys.ps1 or | |
2261 | sys.ps2 to prompt the next line.""" |
|
2259 | sys.ps2 to prompt the next line.""" | |
2262 |
|
2260 | |||
2263 | # We need to ensure that the source is unicode from here on. |
|
2261 | # We need to ensure that the source is unicode from here on. | |
2264 | if type(source)==str: |
|
2262 | if type(source)==str: | |
2265 | usource = source.decode(self.stdin_encoding) |
|
2263 | usource = source.decode(self.stdin_encoding) | |
2266 | else: |
|
2264 | else: | |
2267 | usource = source |
|
2265 | usource = source | |
2268 |
|
2266 | |||
2269 | if 0: # dbg |
|
2267 | if 0: # dbg | |
2270 | print 'Source:', repr(source) # dbg |
|
2268 | print 'Source:', repr(source) # dbg | |
2271 | print 'USource:', repr(usource) # dbg |
|
2269 | print 'USource:', repr(usource) # dbg | |
2272 | print 'type:', type(source) # dbg |
|
2270 | print 'type:', type(source) # dbg | |
2273 | print 'encoding', self.stdin_encoding # dbg |
|
2271 | print 'encoding', self.stdin_encoding # dbg | |
2274 |
|
2272 | |||
2275 | try: |
|
2273 | try: | |
2276 | code = self.compile(usource, symbol, self.execution_count) |
|
2274 | code = self.compile(usource, symbol, self.execution_count) | |
2277 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
2275 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): | |
2278 | # Case 1 |
|
2276 | # Case 1 | |
2279 | self.showsyntaxerror(filename) |
|
2277 | self.showsyntaxerror(filename) | |
2280 | return None |
|
2278 | return None | |
2281 |
|
2279 | |||
2282 | if code is None: |
|
2280 | if code is None: | |
2283 | # Case 2 |
|
2281 | # Case 2 | |
2284 | return True |
|
2282 | return True | |
2285 |
|
2283 | |||
2286 | # Case 3 |
|
2284 | # Case 3 | |
2287 | # We store the code object so that threaded shells and |
|
2285 | # We store the code object so that threaded shells and | |
2288 | # custom exception handlers can access all this info if needed. |
|
2286 | # custom exception handlers can access all this info if needed. | |
2289 | # The source corresponding to this can be obtained from the |
|
2287 | # The source corresponding to this can be obtained from the | |
2290 | # buffer attribute as '\n'.join(self.buffer). |
|
2288 | # buffer attribute as '\n'.join(self.buffer). | |
2291 | self.code_to_run = code |
|
2289 | self.code_to_run = code | |
2292 | # now actually execute the code object |
|
2290 | # now actually execute the code object | |
2293 | if self.run_code(code, post_execute) == 0: |
|
2291 | if self.run_code(code, post_execute) == 0: | |
2294 | return False |
|
2292 | return False | |
2295 | else: |
|
2293 | else: | |
2296 | return None |
|
2294 | return None | |
2297 |
|
2295 | |||
2298 | # For backwards compatibility |
|
2296 | # For backwards compatibility | |
2299 | runsource = run_source |
|
2297 | runsource = run_source | |
2300 |
|
2298 | |||
2301 | def run_code(self, code_obj, post_execute=True): |
|
2299 | def run_code(self, code_obj, post_execute=True): | |
2302 | """Execute a code object. |
|
2300 | """Execute a code object. | |
2303 |
|
2301 | |||
2304 | When an exception occurs, self.showtraceback() is called to display a |
|
2302 | When an exception occurs, self.showtraceback() is called to display a | |
2305 | traceback. |
|
2303 | traceback. | |
2306 |
|
2304 | |||
2307 | Return value: a flag indicating whether the code to be run completed |
|
2305 | Return value: a flag indicating whether the code to be run completed | |
2308 | successfully: |
|
2306 | successfully: | |
2309 |
|
2307 | |||
2310 | - 0: successful execution. |
|
2308 | - 0: successful execution. | |
2311 | - 1: an error occurred. |
|
2309 | - 1: an error occurred. | |
2312 | """ |
|
2310 | """ | |
2313 |
|
2311 | |||
2314 | # Set our own excepthook in case the user code tries to call it |
|
2312 | # Set our own excepthook in case the user code tries to call it | |
2315 | # directly, so that the IPython crash handler doesn't get triggered |
|
2313 | # directly, so that the IPython crash handler doesn't get triggered | |
2316 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2314 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2317 |
|
2315 | |||
2318 | # we save the original sys.excepthook in the instance, in case config |
|
2316 | # we save the original sys.excepthook in the instance, in case config | |
2319 | # code (such as magics) needs access to it. |
|
2317 | # code (such as magics) needs access to it. | |
2320 | self.sys_excepthook = old_excepthook |
|
2318 | self.sys_excepthook = old_excepthook | |
2321 | outflag = 1 # happens in more places, so it's easier as default |
|
2319 | outflag = 1 # happens in more places, so it's easier as default | |
2322 | try: |
|
2320 | try: | |
2323 | try: |
|
2321 | try: | |
2324 | self.hooks.pre_run_code_hook() |
|
2322 | self.hooks.pre_run_code_hook() | |
2325 | #rprint('Running code', repr(code_obj)) # dbg |
|
2323 | #rprint('Running code', repr(code_obj)) # dbg | |
2326 | exec code_obj in self.user_global_ns, self.user_ns |
|
2324 | exec code_obj in self.user_global_ns, self.user_ns | |
2327 | finally: |
|
2325 | finally: | |
2328 | # Reset our crash handler in place |
|
2326 | # Reset our crash handler in place | |
2329 | sys.excepthook = old_excepthook |
|
2327 | sys.excepthook = old_excepthook | |
2330 | except SystemExit: |
|
2328 | except SystemExit: | |
2331 | self.reset_buffer() |
|
2329 | self.reset_buffer() | |
2332 | self.showtraceback(exception_only=True) |
|
2330 | self.showtraceback(exception_only=True) | |
2333 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) |
|
2331 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) | |
2334 | except self.custom_exceptions: |
|
2332 | except self.custom_exceptions: | |
2335 | etype,value,tb = sys.exc_info() |
|
2333 | etype,value,tb = sys.exc_info() | |
2336 | self.CustomTB(etype,value,tb) |
|
2334 | self.CustomTB(etype,value,tb) | |
2337 | except: |
|
2335 | except: | |
2338 | self.showtraceback() |
|
2336 | self.showtraceback() | |
2339 | else: |
|
2337 | else: | |
2340 | outflag = 0 |
|
2338 | outflag = 0 | |
2341 | if softspace(sys.stdout, 0): |
|
2339 | if softspace(sys.stdout, 0): | |
2342 |
|
2340 | |||
2343 |
|
2341 | |||
2344 | # Execute any registered post-execution functions. Here, any errors |
|
2342 | # Execute any registered post-execution functions. Here, any errors | |
2345 | # are reported only minimally and just on the terminal, because the |
|
2343 | # are reported only minimally and just on the terminal, because the | |
2346 | # main exception channel may be occupied with a user traceback. |
|
2344 | # main exception channel may be occupied with a user traceback. | |
2347 | # FIXME: we need to think this mechanism a little more carefully. |
|
2345 | # FIXME: we need to think this mechanism a little more carefully. | |
2348 | if post_execute: |
|
2346 | if post_execute: | |
2349 | for func in self._post_execute: |
|
2347 | for func in self._post_execute: | |
2350 | try: |
|
2348 | try: | |
2351 | func() |
|
2349 | func() | |
2352 | except: |
|
2350 | except: | |
2353 | head = '[ ERROR ] Evaluating post_execute function: %s' % \ |
|
2351 | head = '[ ERROR ] Evaluating post_execute function: %s' % \ | |
2354 | func |
|
2352 | func | |
2355 | print >> io.Term.cout, head |
|
2353 | print >> io.Term.cout, head | |
2356 | print >> io.Term.cout, self._simple_error() |
|
2354 | print >> io.Term.cout, self._simple_error() | |
2357 | print >> io.Term.cout, 'Removing from post_execute' |
|
2355 | print >> io.Term.cout, 'Removing from post_execute' | |
2358 | self._post_execute.remove(func) |
|
2356 | self._post_execute.remove(func) | |
2359 |
|
2357 | |||
2360 | # Flush out code object which has been run (and source) |
|
2358 | # Flush out code object which has been run (and source) | |
2361 | self.code_to_run = None |
|
2359 | self.code_to_run = None | |
2362 | return outflag |
|
2360 | return outflag | |
2363 |
|
2361 | |||
2364 | # For backwards compatibility |
|
2362 | # For backwards compatibility | |
2365 | runcode = run_code |
|
2363 | runcode = run_code | |
2366 |
|
2364 | |||
2367 | # PENDING REMOVAL: this method is slated for deletion, once our new |
|
2365 | # PENDING REMOVAL: this method is slated for deletion, once our new | |
2368 | # input logic has been 100% moved to frontends and is stable. |
|
2366 | # input logic has been 100% moved to frontends and is stable. | |
2369 | def push_line(self, line): |
|
2367 | def push_line(self, line): | |
2370 | """Push a line to the interpreter. |
|
2368 | """Push a line to the interpreter. | |
2371 |
|
2369 | |||
2372 | The line should not have a trailing newline; it may have |
|
2370 | The line should not have a trailing newline; it may have | |
2373 | internal newlines. The line is appended to a buffer and the |
|
2371 | internal newlines. The line is appended to a buffer and the | |
2374 | interpreter's run_source() method is called with the |
|
2372 | interpreter's run_source() method is called with the | |
2375 | concatenated contents of the buffer as source. If this |
|
2373 | concatenated contents of the buffer as source. If this | |
2376 | indicates that the command was executed or invalid, the buffer |
|
2374 | indicates that the command was executed or invalid, the buffer | |
2377 | is reset; otherwise, the command is incomplete, and the buffer |
|
2375 | is reset; otherwise, the command is incomplete, and the buffer | |
2378 | is left as it was after the line was appended. The return |
|
2376 | is left as it was after the line was appended. The return | |
2379 | value is 1 if more input is required, 0 if the line was dealt |
|
2377 | value is 1 if more input is required, 0 if the line was dealt | |
2380 | with in some way (this is the same as run_source()). |
|
2378 | with in some way (this is the same as run_source()). | |
2381 | """ |
|
2379 | """ | |
2382 |
|
2380 | |||
2383 | # autoindent management should be done here, and not in the |
|
2381 | # autoindent management should be done here, and not in the | |
2384 | # interactive loop, since that one is only seen by keyboard input. We |
|
2382 | # interactive loop, since that one is only seen by keyboard input. We | |
2385 | # need this done correctly even for code run via runlines (which uses |
|
2383 | # need this done correctly even for code run via runlines (which uses | |
2386 | # push). |
|
2384 | # push). | |
2387 |
|
2385 | |||
2388 | #print 'push line: <%s>' % line # dbg |
|
2386 | #print 'push line: <%s>' % line # dbg | |
2389 | self.buffer.append(line) |
|
2387 | self.buffer.append(line) | |
2390 | full_source = '\n'.join(self.buffer) |
|
2388 | full_source = '\n'.join(self.buffer) | |
2391 | more = self.run_source(full_source, self.filename) |
|
2389 | more = self.run_source(full_source, self.filename) | |
2392 | if not more: |
|
2390 | if not more: | |
2393 | self.history_manager.store_inputs(self.execution_count, |
|
2391 | self.history_manager.store_inputs(self.execution_count, | |
2394 | '\n'.join(self.buffer_raw), full_source) |
|
2392 | '\n'.join(self.buffer_raw), full_source) | |
2395 | self.reset_buffer() |
|
2393 | self.reset_buffer() | |
2396 | self.execution_count += 1 |
|
2394 | self.execution_count += 1 | |
2397 | return more |
|
2395 | return more | |
2398 |
|
2396 | |||
2399 | def reset_buffer(self): |
|
2397 | def reset_buffer(self): | |
2400 | """Reset the input buffer.""" |
|
2398 | """Reset the input buffer.""" | |
2401 | self.buffer[:] = [] |
|
2399 | self.buffer[:] = [] | |
2402 | self.buffer_raw[:] = [] |
|
2400 | self.buffer_raw[:] = [] | |
2403 | self.input_splitter.reset() |
|
2401 | self.input_splitter.reset() | |
2404 |
|
2402 | |||
2405 | # For backwards compatibility |
|
2403 | # For backwards compatibility | |
2406 | resetbuffer = reset_buffer |
|
2404 | resetbuffer = reset_buffer | |
2407 |
|
2405 | |||
2408 | def _is_secondary_block_start(self, s): |
|
2406 | def _is_secondary_block_start(self, s): | |
2409 | if not s.endswith(':'): |
|
2407 | if not s.endswith(':'): | |
2410 | return False |
|
2408 | return False | |
2411 | if (s.startswith('elif') or |
|
2409 | if (s.startswith('elif') or | |
2412 | s.startswith('else') or |
|
2410 | s.startswith('else') or | |
2413 | s.startswith('except') or |
|
2411 | s.startswith('except') or | |
2414 | s.startswith('finally')): |
|
2412 | s.startswith('finally')): | |
2415 | return True |
|
2413 | return True | |
2416 |
|
2414 | |||
2417 | def _cleanup_ipy_script(self, script): |
|
2415 | def _cleanup_ipy_script(self, script): | |
2418 | """Make a script safe for self.runlines() |
|
2416 | """Make a script safe for self.runlines() | |
2419 |
|
2417 | |||
2420 | Currently, IPython is lines based, with blocks being detected by |
|
2418 | Currently, IPython is lines based, with blocks being detected by | |
2421 | empty lines. This is a problem for block based scripts that may |
|
2419 | empty lines. This is a problem for block based scripts that may | |
2422 | not have empty lines after blocks. This script adds those empty |
|
2420 | not have empty lines after blocks. This script adds those empty | |
2423 | lines to make scripts safe for running in the current line based |
|
2421 | lines to make scripts safe for running in the current line based | |
2424 | IPython. |
|
2422 | IPython. | |
2425 | """ |
|
2423 | """ | |
2426 | res = [] |
|
2424 | res = [] | |
2427 | lines = script.splitlines() |
|
2425 | lines = script.splitlines() | |
2428 | level = 0 |
|
2426 | level = 0 | |
2429 |
|
2427 | |||
2430 | for l in lines: |
|
2428 | for l in lines: | |
2431 | lstripped = l.lstrip() |
|
2429 | lstripped = l.lstrip() | |
2432 | stripped = l.strip() |
|
2430 | stripped = l.strip() | |
2433 | if not stripped: |
|
2431 | if not stripped: | |
2434 | continue |
|
2432 | continue | |
2435 | newlevel = len(l) - len(lstripped) |
|
2433 | newlevel = len(l) - len(lstripped) | |
2436 | if level > 0 and newlevel == 0 and \ |
|
2434 | if level > 0 and newlevel == 0 and \ | |
2437 | not self._is_secondary_block_start(stripped): |
|
2435 | not self._is_secondary_block_start(stripped): | |
2438 | # add empty line |
|
2436 | # add empty line | |
2439 | res.append('') |
|
2437 | res.append('') | |
2440 | res.append(l) |
|
2438 | res.append(l) | |
2441 | level = newlevel |
|
2439 | level = newlevel | |
2442 |
|
2440 | |||
2443 | return '\n'.join(res) + '\n' |
|
2441 | return '\n'.join(res) + '\n' | |
2444 |
|
2442 | |||
2445 | #------------------------------------------------------------------------- |
|
2443 | #------------------------------------------------------------------------- | |
2446 | # Things related to GUI support and pylab |
|
2444 | # Things related to GUI support and pylab | |
2447 | #------------------------------------------------------------------------- |
|
2445 | #------------------------------------------------------------------------- | |
2448 |
|
2446 | |||
2449 | def enable_pylab(self, gui=None): |
|
2447 | def enable_pylab(self, gui=None): | |
2450 | raise NotImplementedError('Implement enable_pylab in a subclass') |
|
2448 | raise NotImplementedError('Implement enable_pylab in a subclass') | |
2451 |
|
2449 | |||
2452 | #------------------------------------------------------------------------- |
|
2450 | #------------------------------------------------------------------------- | |
2453 | # Utilities |
|
2451 | # Utilities | |
2454 | #------------------------------------------------------------------------- |
|
2452 | #------------------------------------------------------------------------- | |
2455 |
|
2453 | |||
2456 | def var_expand(self,cmd,depth=0): |
|
2454 | def var_expand(self,cmd,depth=0): | |
2457 | """Expand python variables in a string. |
|
2455 | """Expand python variables in a string. | |
2458 |
|
2456 | |||
2459 | The depth argument indicates how many frames above the caller should |
|
2457 | The depth argument indicates how many frames above the caller should | |
2460 | be walked to look for the local namespace where to expand variables. |
|
2458 | be walked to look for the local namespace where to expand variables. | |
2461 |
|
2459 | |||
2462 | The global namespace for expansion is always the user's interactive |
|
2460 | The global namespace for expansion is always the user's interactive | |
2463 | namespace. |
|
2461 | namespace. | |
2464 | """ |
|
2462 | """ | |
2465 |
|
2463 | |||
2466 | return str(ItplNS(cmd, |
|
2464 | return str(ItplNS(cmd, | |
2467 | self.user_ns, # globals |
|
2465 | self.user_ns, # globals | |
2468 | # Skip our own frame in searching for locals: |
|
2466 | # Skip our own frame in searching for locals: | |
2469 | sys._getframe(depth+1).f_locals # locals |
|
2467 | sys._getframe(depth+1).f_locals # locals | |
2470 | )) |
|
2468 | )) | |
2471 |
|
2469 | |||
2472 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
2470 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
2473 | """Make a new tempfile and return its filename. |
|
2471 | """Make a new tempfile and return its filename. | |
2474 |
|
2472 | |||
2475 | This makes a call to tempfile.mktemp, but it registers the created |
|
2473 | This makes a call to tempfile.mktemp, but it registers the created | |
2476 | filename internally so ipython cleans it up at exit time. |
|
2474 | filename internally so ipython cleans it up at exit time. | |
2477 |
|
2475 | |||
2478 | Optional inputs: |
|
2476 | Optional inputs: | |
2479 |
|
2477 | |||
2480 | - data(None): if data is given, it gets written out to the temp file |
|
2478 | - data(None): if data is given, it gets written out to the temp file | |
2481 | immediately, and the file is closed again.""" |
|
2479 | immediately, and the file is closed again.""" | |
2482 |
|
2480 | |||
2483 | filename = tempfile.mktemp('.py', prefix) |
|
2481 | filename = tempfile.mktemp('.py', prefix) | |
2484 | self.tempfiles.append(filename) |
|
2482 | self.tempfiles.append(filename) | |
2485 |
|
2483 | |||
2486 | if data: |
|
2484 | if data: | |
2487 | tmp_file = open(filename,'w') |
|
2485 | tmp_file = open(filename,'w') | |
2488 | tmp_file.write(data) |
|
2486 | tmp_file.write(data) | |
2489 | tmp_file.close() |
|
2487 | tmp_file.close() | |
2490 | return filename |
|
2488 | return filename | |
2491 |
|
2489 | |||
2492 | # TODO: This should be removed when Term is refactored. |
|
2490 | # TODO: This should be removed when Term is refactored. | |
2493 | def write(self,data): |
|
2491 | def write(self,data): | |
2494 | """Write a string to the default output""" |
|
2492 | """Write a string to the default output""" | |
2495 | io.Term.cout.write(data) |
|
2493 | io.Term.cout.write(data) | |
2496 |
|
2494 | |||
2497 | # TODO: This should be removed when Term is refactored. |
|
2495 | # TODO: This should be removed when Term is refactored. | |
2498 | def write_err(self,data): |
|
2496 | def write_err(self,data): | |
2499 | """Write a string to the default error output""" |
|
2497 | """Write a string to the default error output""" | |
2500 | io.Term.cerr.write(data) |
|
2498 | io.Term.cerr.write(data) | |
2501 |
|
2499 | |||
2502 | def ask_yes_no(self,prompt,default=True): |
|
2500 | def ask_yes_no(self,prompt,default=True): | |
2503 | if self.quiet: |
|
2501 | if self.quiet: | |
2504 | return True |
|
2502 | return True | |
2505 | return ask_yes_no(prompt,default) |
|
2503 | return ask_yes_no(prompt,default) | |
2506 |
|
2504 | |||
2507 | def show_usage(self): |
|
2505 | def show_usage(self): | |
2508 | """Show a usage message""" |
|
2506 | """Show a usage message""" | |
2509 | page.page(IPython.core.usage.interactive_usage) |
|
2507 | page.page(IPython.core.usage.interactive_usage) | |
2510 |
|
2508 | |||
2511 | #------------------------------------------------------------------------- |
|
2509 | #------------------------------------------------------------------------- | |
2512 | # Things related to IPython exiting |
|
2510 | # Things related to IPython exiting | |
2513 | #------------------------------------------------------------------------- |
|
2511 | #------------------------------------------------------------------------- | |
2514 | def atexit_operations(self): |
|
2512 | def atexit_operations(self): | |
2515 | """This will be executed at the time of exit. |
|
2513 | """This will be executed at the time of exit. | |
2516 |
|
2514 | |||
2517 | Cleanup operations and saving of persistent data that is done |
|
2515 | Cleanup operations and saving of persistent data that is done | |
2518 | unconditionally by IPython should be performed here. |
|
2516 | unconditionally by IPython should be performed here. | |
2519 |
|
2517 | |||
2520 | For things that may depend on startup flags or platform specifics (such |
|
2518 | For things that may depend on startup flags or platform specifics (such | |
2521 | as having readline or not), register a separate atexit function in the |
|
2519 | as having readline or not), register a separate atexit function in the | |
2522 | code that has the appropriate information, rather than trying to |
|
2520 | code that has the appropriate information, rather than trying to | |
2523 | clutter |
|
2521 | clutter | |
2524 | """ |
|
2522 | """ | |
2525 | # Cleanup all tempfiles left around |
|
2523 | # Cleanup all tempfiles left around | |
2526 | for tfile in self.tempfiles: |
|
2524 | for tfile in self.tempfiles: | |
2527 | try: |
|
2525 | try: | |
2528 | os.unlink(tfile) |
|
2526 | os.unlink(tfile) | |
2529 | except OSError: |
|
2527 | except OSError: | |
2530 | pass |
|
2528 | pass | |
2531 |
|
2529 | |||
2532 | # Write anything in the history cache to the database. |
|
2530 | # Write anything in the history cache to the database. | |
2533 | self.history_manager.writeout_cache() |
|
2531 | self.history_manager.writeout_cache() | |
2534 |
|
2532 | |||
2535 | # Clear all user namespaces to release all references cleanly. |
|
2533 | # Clear all user namespaces to release all references cleanly. | |
2536 | self.reset() |
|
2534 | self.reset(new_session=False) | |
2537 |
|
2535 | |||
2538 | # Run user hooks |
|
2536 | # Run user hooks | |
2539 | self.hooks.shutdown_hook() |
|
2537 | self.hooks.shutdown_hook() | |
2540 |
|
2538 | |||
2541 | def cleanup(self): |
|
2539 | def cleanup(self): | |
2542 | self.restore_sys_module_state() |
|
2540 | self.restore_sys_module_state() | |
2543 |
|
2541 | |||
2544 |
|
2542 | |||
2545 | class InteractiveShellABC(object): |
|
2543 | class InteractiveShellABC(object): | |
2546 | """An abstract base class for InteractiveShell.""" |
|
2544 | """An abstract base class for InteractiveShell.""" | |
2547 | __metaclass__ = abc.ABCMeta |
|
2545 | __metaclass__ = abc.ABCMeta | |
2548 |
|
2546 | |||
2549 | InteractiveShellABC.register(InteractiveShell) |
|
2547 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,398 +1,397 b'' | |||||
1 | """Tests for various magic functions. |
|
1 | """Tests for various magic functions. | |
2 |
|
2 | |||
3 | Needs to be run by nose (to make ipython session available). |
|
3 | Needs to be run by nose (to make ipython session available). | |
4 | """ |
|
4 | """ | |
5 | from __future__ import absolute_import |
|
5 | from __future__ import absolute_import | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Imports |
|
8 | # Imports | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | import os |
|
11 | import os | |
12 | import sys |
|
12 | import sys | |
13 | import tempfile |
|
13 | import tempfile | |
14 | import types |
|
14 | import types | |
15 | from cStringIO import StringIO |
|
15 | from cStringIO import StringIO | |
16 |
|
16 | |||
17 | import nose.tools as nt |
|
17 | import nose.tools as nt | |
18 |
|
18 | |||
19 | from IPython.utils.path import get_long_path_name |
|
19 | from IPython.utils.path import get_long_path_name | |
20 | from IPython.testing import decorators as dec |
|
20 | from IPython.testing import decorators as dec | |
21 | from IPython.testing import tools as tt |
|
21 | from IPython.testing import tools as tt | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Test functions begin |
|
24 | # Test functions begin | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | def test_rehashx(): |
|
26 | def test_rehashx(): | |
27 | # clear up everything |
|
27 | # clear up everything | |
28 | _ip = get_ipython() |
|
28 | _ip = get_ipython() | |
29 | _ip.alias_manager.alias_table.clear() |
|
29 | _ip.alias_manager.alias_table.clear() | |
30 | del _ip.db['syscmdlist'] |
|
30 | del _ip.db['syscmdlist'] | |
31 |
|
31 | |||
32 | _ip.magic('rehashx') |
|
32 | _ip.magic('rehashx') | |
33 | # Practically ALL ipython development systems will have more than 10 aliases |
|
33 | # Practically ALL ipython development systems will have more than 10 aliases | |
34 |
|
34 | |||
35 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) |
|
35 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) | |
36 | for key, val in _ip.alias_manager.alias_table.iteritems(): |
|
36 | for key, val in _ip.alias_manager.alias_table.iteritems(): | |
37 | # we must strip dots from alias names |
|
37 | # we must strip dots from alias names | |
38 | nt.assert_true('.' not in key) |
|
38 | nt.assert_true('.' not in key) | |
39 |
|
39 | |||
40 | # rehashx must fill up syscmdlist |
|
40 | # rehashx must fill up syscmdlist | |
41 | scoms = _ip.db['syscmdlist'] |
|
41 | scoms = _ip.db['syscmdlist'] | |
42 | yield (nt.assert_true, len(scoms) > 10) |
|
42 | yield (nt.assert_true, len(scoms) > 10) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def test_magic_parse_options(): |
|
45 | def test_magic_parse_options(): | |
46 | """Test that we don't mangle paths when parsing magic options.""" |
|
46 | """Test that we don't mangle paths when parsing magic options.""" | |
47 | ip = get_ipython() |
|
47 | ip = get_ipython() | |
48 | path = 'c:\\x' |
|
48 | path = 'c:\\x' | |
49 | opts = ip.parse_options('-f %s' % path,'f:')[0] |
|
49 | opts = ip.parse_options('-f %s' % path,'f:')[0] | |
50 | # argv splitting is os-dependent |
|
50 | # argv splitting is os-dependent | |
51 | if os.name == 'posix': |
|
51 | if os.name == 'posix': | |
52 | expected = 'c:x' |
|
52 | expected = 'c:x' | |
53 | else: |
|
53 | else: | |
54 | expected = path |
|
54 | expected = path | |
55 | nt.assert_equals(opts['f'], expected) |
|
55 | nt.assert_equals(opts['f'], expected) | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | def doctest_hist_f(): |
|
58 | def doctest_hist_f(): | |
59 | """Test %hist -f with temporary filename. |
|
59 | """Test %hist -f with temporary filename. | |
60 |
|
60 | |||
61 | In [9]: import tempfile |
|
61 | In [9]: import tempfile | |
62 |
|
62 | |||
63 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
63 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') | |
64 |
|
64 | |||
65 | In [11]: %hist -nl -f $tfile 3 |
|
65 | In [11]: %hist -nl -f $tfile 3 | |
66 |
|
66 | |||
67 | In [13]: import os; os.unlink(tfile) |
|
67 | In [13]: import os; os.unlink(tfile) | |
68 | """ |
|
68 | """ | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | def doctest_hist_r(): |
|
71 | def doctest_hist_r(): | |
72 | """Test %hist -r |
|
72 | """Test %hist -r | |
73 |
|
73 | |||
74 | XXX - This test is not recording the output correctly. For some reason, in |
|
74 | XXX - This test is not recording the output correctly. For some reason, in | |
75 | testing mode the raw history isn't getting populated. No idea why. |
|
75 | testing mode the raw history isn't getting populated. No idea why. | |
76 | Disabling the output checking for now, though at least we do run it. |
|
76 | Disabling the output checking for now, though at least we do run it. | |
77 |
|
77 | |||
78 | In [1]: 'hist' in _ip.lsmagic() |
|
78 | In [1]: 'hist' in _ip.lsmagic() | |
79 | Out[1]: True |
|
79 | Out[1]: True | |
80 |
|
80 | |||
81 | In [2]: x=1 |
|
81 | In [2]: x=1 | |
82 |
|
82 | |||
83 | In [3]: %hist -rl 2 |
|
83 | In [3]: %hist -rl 2 | |
84 | x=1 # random |
|
84 | x=1 # random | |
85 | %hist -r 2 |
|
85 | %hist -r 2 | |
86 | """ |
|
86 | """ | |
87 |
|
87 | |||
88 | def doctest_hist_op(): |
|
88 | def doctest_hist_op(): | |
89 | """Test %hist -op |
|
89 | """Test %hist -op | |
90 |
|
90 | |||
91 | In [1]: class b: |
|
91 | In [1]: class b: | |
92 | ...: pass |
|
92 | ...: pass | |
93 | ...: |
|
93 | ...: | |
94 |
|
94 | |||
95 | In [2]: class s(b): |
|
95 | In [2]: class s(b): | |
96 | ...: def __str__(self): |
|
96 | ...: def __str__(self): | |
97 | ...: return 's' |
|
97 | ...: return 's' | |
98 | ...: |
|
98 | ...: | |
99 |
|
99 | |||
100 | In [3]: |
|
100 | In [3]: | |
101 |
|
101 | |||
102 | In [4]: class r(b): |
|
102 | In [4]: class r(b): | |
103 | ...: def __repr__(self): |
|
103 | ...: def __repr__(self): | |
104 | ...: return 'r' |
|
104 | ...: return 'r' | |
105 | ...: |
|
105 | ...: | |
106 |
|
106 | |||
107 | In [5]: class sr(s,r): pass |
|
107 | In [5]: class sr(s,r): pass | |
108 | ...: |
|
108 | ...: | |
109 |
|
109 | |||
110 | In [6]: |
|
110 | In [6]: | |
111 |
|
111 | |||
112 | In [7]: bb=b() |
|
112 | In [7]: bb=b() | |
113 |
|
113 | |||
114 | In [8]: ss=s() |
|
114 | In [8]: ss=s() | |
115 |
|
115 | |||
116 | In [9]: rr=r() |
|
116 | In [9]: rr=r() | |
117 |
|
117 | |||
118 | In [10]: ssrr=sr() |
|
118 | In [10]: ssrr=sr() | |
119 |
|
119 | |||
120 | In [11]: bb |
|
120 | In [11]: bb | |
121 | Out[11]: <...b instance at ...> |
|
121 | Out[11]: <...b instance at ...> | |
122 |
|
122 | |||
123 | In [12]: ss |
|
123 | In [12]: ss | |
124 | Out[12]: <...s instance at ...> |
|
124 | Out[12]: <...s instance at ...> | |
125 |
|
125 | |||
126 | In [13]: |
|
126 | In [13]: | |
127 |
|
127 | |||
128 | In [14]: %hist -op |
|
128 | In [14]: %hist -op | |
129 | >>> class b: |
|
129 | >>> class b: | |
130 | ... pass |
|
130 | ... pass | |
131 | ... |
|
131 | ... | |
132 | >>> class s(b): |
|
132 | >>> class s(b): | |
133 | ... def __str__(self): |
|
133 | ... def __str__(self): | |
134 | ... return 's' |
|
134 | ... return 's' | |
135 | ... |
|
135 | ... | |
136 | >>> |
|
136 | >>> | |
137 | >>> class r(b): |
|
137 | >>> class r(b): | |
138 | ... def __repr__(self): |
|
138 | ... def __repr__(self): | |
139 | ... return 'r' |
|
139 | ... return 'r' | |
140 | ... |
|
140 | ... | |
141 | >>> class sr(s,r): pass |
|
141 | >>> class sr(s,r): pass | |
142 | >>> |
|
142 | >>> | |
143 | >>> bb=b() |
|
143 | >>> bb=b() | |
144 | >>> ss=s() |
|
144 | >>> ss=s() | |
145 | >>> rr=r() |
|
145 | >>> rr=r() | |
146 | >>> ssrr=sr() |
|
146 | >>> ssrr=sr() | |
147 | >>> bb |
|
147 | >>> bb | |
148 | <...b instance at ...> |
|
148 | <...b instance at ...> | |
149 | >>> ss |
|
149 | >>> ss | |
150 | <...s instance at ...> |
|
150 | <...s instance at ...> | |
151 | >>> |
|
151 | >>> | |
152 | """ |
|
152 | """ | |
153 |
|
153 | |||
154 | def test_macro(): |
|
154 | def test_macro(): | |
155 | ip = get_ipython() |
|
155 | ip = get_ipython() | |
156 | ip.history_manager.reset() # Clear any existing history. |
|
156 | ip.history_manager.reset() # Clear any existing history. | |
157 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] |
|
157 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] | |
158 | for i, cmd in enumerate(cmds, start=1): |
|
158 | for i, cmd in enumerate(cmds, start=1): | |
159 | ip.history_manager.store_inputs(i, cmd) |
|
159 | ip.history_manager.store_inputs(i, cmd) | |
160 | print i, cmd |
|
|||
161 | ip.magic("macro test 1-3") |
|
160 | ip.magic("macro test 1-3") | |
162 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") |
|
161 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") | |
163 |
|
162 | |||
164 | # List macros. |
|
163 | # List macros. | |
165 | assert "test" in ip.magic("macro") |
|
164 | assert "test" in ip.magic("macro") | |
166 |
|
165 | |||
167 |
|
166 | |||
168 | # XXX failing for now, until we get clearcmd out of quarantine. But we should |
|
167 | # XXX failing for now, until we get clearcmd out of quarantine. But we should | |
169 | # fix this and revert the skip to happen only if numpy is not around. |
|
168 | # fix this and revert the skip to happen only if numpy is not around. | |
170 | #@dec.skipif_not_numpy |
|
169 | #@dec.skipif_not_numpy | |
171 | @dec.skip_known_failure |
|
170 | @dec.skip_known_failure | |
172 | def test_numpy_clear_array_undec(): |
|
171 | def test_numpy_clear_array_undec(): | |
173 | from IPython.extensions import clearcmd |
|
172 | from IPython.extensions import clearcmd | |
174 |
|
173 | |||
175 | _ip.ex('import numpy as np') |
|
174 | _ip.ex('import numpy as np') | |
176 | _ip.ex('a = np.empty(2)') |
|
175 | _ip.ex('a = np.empty(2)') | |
177 | yield (nt.assert_true, 'a' in _ip.user_ns) |
|
176 | yield (nt.assert_true, 'a' in _ip.user_ns) | |
178 | _ip.magic('clear array') |
|
177 | _ip.magic('clear array') | |
179 | yield (nt.assert_false, 'a' in _ip.user_ns) |
|
178 | yield (nt.assert_false, 'a' in _ip.user_ns) | |
180 |
|
179 | |||
181 |
|
180 | |||
182 | # Multiple tests for clipboard pasting |
|
181 | # Multiple tests for clipboard pasting | |
183 | @dec.parametric |
|
182 | @dec.parametric | |
184 | def test_paste(): |
|
183 | def test_paste(): | |
185 | _ip = get_ipython() |
|
184 | _ip = get_ipython() | |
186 | def paste(txt, flags='-q'): |
|
185 | def paste(txt, flags='-q'): | |
187 | """Paste input text, by default in quiet mode""" |
|
186 | """Paste input text, by default in quiet mode""" | |
188 | hooks.clipboard_get = lambda : txt |
|
187 | hooks.clipboard_get = lambda : txt | |
189 | _ip.magic('paste '+flags) |
|
188 | _ip.magic('paste '+flags) | |
190 |
|
189 | |||
191 | # Inject fake clipboard hook but save original so we can restore it later |
|
190 | # Inject fake clipboard hook but save original so we can restore it later | |
192 | hooks = _ip.hooks |
|
191 | hooks = _ip.hooks | |
193 | user_ns = _ip.user_ns |
|
192 | user_ns = _ip.user_ns | |
194 | original_clip = hooks.clipboard_get |
|
193 | original_clip = hooks.clipboard_get | |
195 |
|
194 | |||
196 | try: |
|
195 | try: | |
197 | # This try/except with an emtpy except clause is here only because |
|
196 | # This try/except with an emtpy except clause is here only because | |
198 | # try/yield/finally is invalid syntax in Python 2.4. This will be |
|
197 | # try/yield/finally is invalid syntax in Python 2.4. This will be | |
199 | # removed when we drop 2.4-compatibility, and the emtpy except below |
|
198 | # removed when we drop 2.4-compatibility, and the emtpy except below | |
200 | # will be changed to a finally. |
|
199 | # will be changed to a finally. | |
201 |
|
200 | |||
202 | # Run tests with fake clipboard function |
|
201 | # Run tests with fake clipboard function | |
203 | user_ns.pop('x', None) |
|
202 | user_ns.pop('x', None) | |
204 | paste('x=1') |
|
203 | paste('x=1') | |
205 | yield nt.assert_equal(user_ns['x'], 1) |
|
204 | yield nt.assert_equal(user_ns['x'], 1) | |
206 |
|
205 | |||
207 | user_ns.pop('x', None) |
|
206 | user_ns.pop('x', None) | |
208 | paste('>>> x=2') |
|
207 | paste('>>> x=2') | |
209 | yield nt.assert_equal(user_ns['x'], 2) |
|
208 | yield nt.assert_equal(user_ns['x'], 2) | |
210 |
|
209 | |||
211 | paste(""" |
|
210 | paste(""" | |
212 | >>> x = [1,2,3] |
|
211 | >>> x = [1,2,3] | |
213 | >>> y = [] |
|
212 | >>> y = [] | |
214 | >>> for i in x: |
|
213 | >>> for i in x: | |
215 | ... y.append(i**2) |
|
214 | ... y.append(i**2) | |
216 | ... |
|
215 | ... | |
217 | """) |
|
216 | """) | |
218 | yield nt.assert_equal(user_ns['x'], [1,2,3]) |
|
217 | yield nt.assert_equal(user_ns['x'], [1,2,3]) | |
219 | yield nt.assert_equal(user_ns['y'], [1,4,9]) |
|
218 | yield nt.assert_equal(user_ns['y'], [1,4,9]) | |
220 |
|
219 | |||
221 | # Now, test that paste -r works |
|
220 | # Now, test that paste -r works | |
222 | user_ns.pop('x', None) |
|
221 | user_ns.pop('x', None) | |
223 | yield nt.assert_false('x' in user_ns) |
|
222 | yield nt.assert_false('x' in user_ns) | |
224 | _ip.magic('paste -r') |
|
223 | _ip.magic('paste -r') | |
225 | yield nt.assert_equal(user_ns['x'], [1,2,3]) |
|
224 | yield nt.assert_equal(user_ns['x'], [1,2,3]) | |
226 |
|
225 | |||
227 | # Also test paste echoing, by temporarily faking the writer |
|
226 | # Also test paste echoing, by temporarily faking the writer | |
228 | w = StringIO() |
|
227 | w = StringIO() | |
229 | writer = _ip.write |
|
228 | writer = _ip.write | |
230 | _ip.write = w.write |
|
229 | _ip.write = w.write | |
231 | code = """ |
|
230 | code = """ | |
232 | a = 100 |
|
231 | a = 100 | |
233 | b = 200""" |
|
232 | b = 200""" | |
234 | try: |
|
233 | try: | |
235 | paste(code,'') |
|
234 | paste(code,'') | |
236 | out = w.getvalue() |
|
235 | out = w.getvalue() | |
237 | finally: |
|
236 | finally: | |
238 | _ip.write = writer |
|
237 | _ip.write = writer | |
239 | yield nt.assert_equal(user_ns['a'], 100) |
|
238 | yield nt.assert_equal(user_ns['a'], 100) | |
240 | yield nt.assert_equal(user_ns['b'], 200) |
|
239 | yield nt.assert_equal(user_ns['b'], 200) | |
241 | yield nt.assert_equal(out, code+"\n## -- End pasted text --\n") |
|
240 | yield nt.assert_equal(out, code+"\n## -- End pasted text --\n") | |
242 |
|
241 | |||
243 | finally: |
|
242 | finally: | |
244 | # This should be in a finally clause, instead of the bare except above. |
|
243 | # This should be in a finally clause, instead of the bare except above. | |
245 | # Restore original hook |
|
244 | # Restore original hook | |
246 | hooks.clipboard_get = original_clip |
|
245 | hooks.clipboard_get = original_clip | |
247 |
|
246 | |||
248 |
|
247 | |||
249 | def test_time(): |
|
248 | def test_time(): | |
250 | _ip.magic('time None') |
|
249 | _ip.magic('time None') | |
251 |
|
250 | |||
252 |
|
251 | |||
253 | def doctest_time(): |
|
252 | def doctest_time(): | |
254 | """ |
|
253 | """ | |
255 | In [10]: %time None |
|
254 | In [10]: %time None | |
256 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
255 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
257 | Wall time: 0.00 s |
|
256 | Wall time: 0.00 s | |
258 | """ |
|
257 | """ | |
259 |
|
258 | |||
260 |
|
259 | |||
261 | def test_doctest_mode(): |
|
260 | def test_doctest_mode(): | |
262 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
|
261 | "Toggle doctest_mode twice, it should be a no-op and run without error" | |
263 | _ip.magic('doctest_mode') |
|
262 | _ip.magic('doctest_mode') | |
264 | _ip.magic('doctest_mode') |
|
263 | _ip.magic('doctest_mode') | |
265 |
|
264 | |||
266 |
|
265 | |||
267 | def test_parse_options(): |
|
266 | def test_parse_options(): | |
268 | """Tests for basic options parsing in magics.""" |
|
267 | """Tests for basic options parsing in magics.""" | |
269 | # These are only the most minimal of tests, more should be added later. At |
|
268 | # These are only the most minimal of tests, more should be added later. At | |
270 | # the very least we check that basic text/unicode calls work OK. |
|
269 | # the very least we check that basic text/unicode calls work OK. | |
271 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') |
|
270 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') | |
272 | nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo') |
|
271 | nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo') | |
273 |
|
272 | |||
274 |
|
273 | |||
275 | def test_dirops(): |
|
274 | def test_dirops(): | |
276 | """Test various directory handling operations.""" |
|
275 | """Test various directory handling operations.""" | |
277 | curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/') |
|
276 | curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/') | |
278 |
|
277 | |||
279 | startdir = os.getcwd() |
|
278 | startdir = os.getcwd() | |
280 | ipdir = _ip.ipython_dir |
|
279 | ipdir = _ip.ipython_dir | |
281 | try: |
|
280 | try: | |
282 | _ip.magic('cd "%s"' % ipdir) |
|
281 | _ip.magic('cd "%s"' % ipdir) | |
283 | nt.assert_equal(curpath(), ipdir) |
|
282 | nt.assert_equal(curpath(), ipdir) | |
284 | _ip.magic('cd -') |
|
283 | _ip.magic('cd -') | |
285 | nt.assert_equal(curpath(), startdir) |
|
284 | nt.assert_equal(curpath(), startdir) | |
286 | _ip.magic('pushd "%s"' % ipdir) |
|
285 | _ip.magic('pushd "%s"' % ipdir) | |
287 | nt.assert_equal(curpath(), ipdir) |
|
286 | nt.assert_equal(curpath(), ipdir) | |
288 | _ip.magic('popd') |
|
287 | _ip.magic('popd') | |
289 | nt.assert_equal(curpath(), startdir) |
|
288 | nt.assert_equal(curpath(), startdir) | |
290 | finally: |
|
289 | finally: | |
291 | os.chdir(startdir) |
|
290 | os.chdir(startdir) | |
292 |
|
291 | |||
293 |
|
292 | |||
294 | def check_cpaste(code, should_fail=False): |
|
293 | def check_cpaste(code, should_fail=False): | |
295 | """Execute code via 'cpaste' and ensure it was executed, unless |
|
294 | """Execute code via 'cpaste' and ensure it was executed, unless | |
296 | should_fail is set. |
|
295 | should_fail is set. | |
297 | """ |
|
296 | """ | |
298 | _ip.user_ns['code_ran'] = False |
|
297 | _ip.user_ns['code_ran'] = False | |
299 |
|
298 | |||
300 | src = StringIO() |
|
299 | src = StringIO() | |
301 | src.write('\n') |
|
300 | src.write('\n') | |
302 | src.write(code) |
|
301 | src.write(code) | |
303 | src.write('\n--\n') |
|
302 | src.write('\n--\n') | |
304 | src.seek(0) |
|
303 | src.seek(0) | |
305 |
|
304 | |||
306 | stdin_save = sys.stdin |
|
305 | stdin_save = sys.stdin | |
307 | sys.stdin = src |
|
306 | sys.stdin = src | |
308 |
|
307 | |||
309 | try: |
|
308 | try: | |
310 | _ip.magic('cpaste') |
|
309 | _ip.magic('cpaste') | |
311 | except: |
|
310 | except: | |
312 | if not should_fail: |
|
311 | if not should_fail: | |
313 | raise AssertionError("Failure not expected : '%s'" % |
|
312 | raise AssertionError("Failure not expected : '%s'" % | |
314 | code) |
|
313 | code) | |
315 | else: |
|
314 | else: | |
316 | assert _ip.user_ns['code_ran'] |
|
315 | assert _ip.user_ns['code_ran'] | |
317 | if should_fail: |
|
316 | if should_fail: | |
318 | raise AssertionError("Failure expected : '%s'" % code) |
|
317 | raise AssertionError("Failure expected : '%s'" % code) | |
319 | finally: |
|
318 | finally: | |
320 | sys.stdin = stdin_save |
|
319 | sys.stdin = stdin_save | |
321 |
|
320 | |||
322 |
|
321 | |||
323 | def test_cpaste(): |
|
322 | def test_cpaste(): | |
324 | """Test cpaste magic""" |
|
323 | """Test cpaste magic""" | |
325 |
|
324 | |||
326 | def run(): |
|
325 | def run(): | |
327 | """Marker function: sets a flag when executed. |
|
326 | """Marker function: sets a flag when executed. | |
328 | """ |
|
327 | """ | |
329 | _ip.user_ns['code_ran'] = True |
|
328 | _ip.user_ns['code_ran'] = True | |
330 | return 'run' # return string so '+ run()' doesn't result in success |
|
329 | return 'run' # return string so '+ run()' doesn't result in success | |
331 |
|
330 | |||
332 | tests = {'pass': ["> > > run()", |
|
331 | tests = {'pass': ["> > > run()", | |
333 | ">>> > run()", |
|
332 | ">>> > run()", | |
334 | "+++ run()", |
|
333 | "+++ run()", | |
335 | "++ run()", |
|
334 | "++ run()", | |
336 | " >>> run()"], |
|
335 | " >>> run()"], | |
337 |
|
336 | |||
338 | 'fail': ["+ + run()", |
|
337 | 'fail': ["+ + run()", | |
339 | " ++ run()"]} |
|
338 | " ++ run()"]} | |
340 |
|
339 | |||
341 | _ip.user_ns['run'] = run |
|
340 | _ip.user_ns['run'] = run | |
342 |
|
341 | |||
343 | for code in tests['pass']: |
|
342 | for code in tests['pass']: | |
344 | check_cpaste(code) |
|
343 | check_cpaste(code) | |
345 |
|
344 | |||
346 | for code in tests['fail']: |
|
345 | for code in tests['fail']: | |
347 | check_cpaste(code, should_fail=True) |
|
346 | check_cpaste(code, should_fail=True) | |
348 |
|
347 | |||
349 | def test_xmode(): |
|
348 | def test_xmode(): | |
350 | # Calling xmode three times should be a no-op |
|
349 | # Calling xmode three times should be a no-op | |
351 | xmode = _ip.InteractiveTB.mode |
|
350 | xmode = _ip.InteractiveTB.mode | |
352 | for i in range(3): |
|
351 | for i in range(3): | |
353 | _ip.magic("xmode") |
|
352 | _ip.magic("xmode") | |
354 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) |
|
353 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) | |
355 |
|
354 | |||
356 | def doctest_who(): |
|
355 | def doctest_who(): | |
357 | """doctest for %who |
|
356 | """doctest for %who | |
358 |
|
357 | |||
359 | In [1]: %reset -f |
|
358 | In [1]: %reset -f | |
360 |
|
359 | |||
361 | In [2]: alpha = 123 |
|
360 | In [2]: alpha = 123 | |
362 |
|
361 | |||
363 | In [3]: beta = 'beta' |
|
362 | In [3]: beta = 'beta' | |
364 |
|
363 | |||
365 | In [4]: %who int |
|
364 | In [4]: %who int | |
366 | alpha |
|
365 | alpha | |
367 |
|
366 | |||
368 | In [5]: %who str |
|
367 | In [5]: %who str | |
369 | beta |
|
368 | beta | |
370 |
|
369 | |||
371 | In [6]: %whos |
|
370 | In [6]: %whos | |
372 | Variable Type Data/Info |
|
371 | Variable Type Data/Info | |
373 | ---------------------------- |
|
372 | ---------------------------- | |
374 | alpha int 123 |
|
373 | alpha int 123 | |
375 | beta str beta |
|
374 | beta str beta | |
376 |
|
375 | |||
377 | In [7]: %who_ls |
|
376 | In [7]: %who_ls | |
378 | Out[7]: ['alpha', 'beta'] |
|
377 | Out[7]: ['alpha', 'beta'] | |
379 | """ |
|
378 | """ | |
380 |
|
379 | |||
381 | def doctest_precision(): |
|
380 | def doctest_precision(): | |
382 | """doctest for %precision |
|
381 | """doctest for %precision | |
383 |
|
382 | |||
384 | In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain'] |
|
383 | In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain'] | |
385 |
|
384 | |||
386 | In [2]: %precision 5 |
|
385 | In [2]: %precision 5 | |
387 | Out[2]: '%.5f' |
|
386 | Out[2]: '%.5f' | |
388 |
|
387 | |||
389 | In [3]: f.float_format |
|
388 | In [3]: f.float_format | |
390 | Out[3]: '%.5f' |
|
389 | Out[3]: '%.5f' | |
391 |
|
390 | |||
392 | In [4]: %precision %e |
|
391 | In [4]: %precision %e | |
393 | Out[4]: '%e' |
|
392 | Out[4]: '%e' | |
394 |
|
393 | |||
395 | In [5]: f(3.1415927) |
|
394 | In [5]: f(3.1415927) | |
396 | Out[5]: '3.141593e+00' |
|
395 | Out[5]: '3.141593e+00' | |
397 | """ |
|
396 | """ | |
398 |
|
397 |
@@ -1,494 +1,490 b'' | |||||
1 | """ A FrontendWidget that emulates the interface of the console IPython and |
|
1 | """ A FrontendWidget that emulates the interface of the console IPython and | |
2 | supports the additional functionality provided by the IPython kernel. |
|
2 | supports the additional functionality provided by the IPython kernel. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Imports |
|
6 | # Imports | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 |
|
8 | |||
9 | # Standard library imports |
|
9 | # Standard library imports | |
10 | from collections import namedtuple |
|
10 | from collections import namedtuple | |
11 | import re |
|
11 | import re | |
12 | from subprocess import Popen |
|
12 | from subprocess import Popen | |
13 | from textwrap import dedent |
|
13 | from textwrap import dedent | |
14 |
|
14 | |||
15 | # System library imports |
|
15 | # System library imports | |
16 | from IPython.external.qt import QtCore, QtGui |
|
16 | from IPython.external.qt import QtCore, QtGui | |
17 |
|
17 | |||
18 | # Local imports |
|
18 | # Local imports | |
19 | from IPython.core.inputsplitter import IPythonInputSplitter, \ |
|
19 | from IPython.core.inputsplitter import IPythonInputSplitter, \ | |
20 | transform_ipy_prompt |
|
20 | transform_ipy_prompt | |
21 | from IPython.core.usage import default_gui_banner |
|
21 | from IPython.core.usage import default_gui_banner | |
22 | from IPython.utils.traitlets import Bool, Str |
|
22 | from IPython.utils.traitlets import Bool, Str | |
23 | from frontend_widget import FrontendWidget |
|
23 | from frontend_widget import FrontendWidget | |
24 | from styles import (default_light_style_sheet, default_light_syntax_style, |
|
24 | from styles import (default_light_style_sheet, default_light_syntax_style, | |
25 | default_dark_style_sheet, default_dark_syntax_style, |
|
25 | default_dark_style_sheet, default_dark_syntax_style, | |
26 | default_bw_style_sheet, default_bw_syntax_style) |
|
26 | default_bw_style_sheet, default_bw_syntax_style) | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Constants |
|
29 | # Constants | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 | # Default strings to build and display input and output prompts (and separators |
|
32 | # Default strings to build and display input and output prompts (and separators | |
33 | # in between) |
|
33 | # in between) | |
34 | default_in_prompt = 'In [<span class="in-prompt-number">%i</span>]: ' |
|
34 | default_in_prompt = 'In [<span class="in-prompt-number">%i</span>]: ' | |
35 | default_out_prompt = 'Out[<span class="out-prompt-number">%i</span>]: ' |
|
35 | default_out_prompt = 'Out[<span class="out-prompt-number">%i</span>]: ' | |
36 | default_input_sep = '\n' |
|
36 | default_input_sep = '\n' | |
37 | default_output_sep = '' |
|
37 | default_output_sep = '' | |
38 | default_output_sep2 = '' |
|
38 | default_output_sep2 = '' | |
39 |
|
39 | |||
40 | # Base path for most payload sources. |
|
40 | # Base path for most payload sources. | |
41 | zmq_shell_source = 'IPython.zmq.zmqshell.ZMQInteractiveShell' |
|
41 | zmq_shell_source = 'IPython.zmq.zmqshell.ZMQInteractiveShell' | |
42 |
|
42 | |||
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 | # IPythonWidget class |
|
44 | # IPythonWidget class | |
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 |
|
46 | |||
47 | class IPythonWidget(FrontendWidget): |
|
47 | class IPythonWidget(FrontendWidget): | |
48 | """ A FrontendWidget for an IPython kernel. |
|
48 | """ A FrontendWidget for an IPython kernel. | |
49 | """ |
|
49 | """ | |
50 |
|
50 | |||
51 | # If set, the 'custom_edit_requested(str, int)' signal will be emitted when |
|
51 | # If set, the 'custom_edit_requested(str, int)' signal will be emitted when | |
52 | # an editor is needed for a file. This overrides 'editor' and 'editor_line' |
|
52 | # an editor is needed for a file. This overrides 'editor' and 'editor_line' | |
53 | # settings. |
|
53 | # settings. | |
54 | custom_edit = Bool(False) |
|
54 | custom_edit = Bool(False) | |
55 | custom_edit_requested = QtCore.Signal(object, object) |
|
55 | custom_edit_requested = QtCore.Signal(object, object) | |
56 |
|
56 | |||
57 | # A command for invoking a system text editor. If the string contains a |
|
57 | # A command for invoking a system text editor. If the string contains a | |
58 | # {filename} format specifier, it will be used. Otherwise, the filename will |
|
58 | # {filename} format specifier, it will be used. Otherwise, the filename will | |
59 | # be appended to the end the command. |
|
59 | # be appended to the end the command. | |
60 | editor = Str('default', config=True) |
|
60 | editor = Str('default', config=True) | |
61 |
|
61 | |||
62 | # The editor command to use when a specific line number is requested. The |
|
62 | # The editor command to use when a specific line number is requested. The | |
63 | # string should contain two format specifiers: {line} and {filename}. If |
|
63 | # string should contain two format specifiers: {line} and {filename}. If | |
64 | # this parameter is not specified, the line number option to the %edit magic |
|
64 | # this parameter is not specified, the line number option to the %edit magic | |
65 | # will be ignored. |
|
65 | # will be ignored. | |
66 | editor_line = Str(config=True) |
|
66 | editor_line = Str(config=True) | |
67 |
|
67 | |||
68 | # A CSS stylesheet. The stylesheet can contain classes for: |
|
68 | # A CSS stylesheet. The stylesheet can contain classes for: | |
69 | # 1. Qt: QPlainTextEdit, QFrame, QWidget, etc |
|
69 | # 1. Qt: QPlainTextEdit, QFrame, QWidget, etc | |
70 | # 2. Pygments: .c, .k, .o, etc (see PygmentsHighlighter) |
|
70 | # 2. Pygments: .c, .k, .o, etc (see PygmentsHighlighter) | |
71 | # 3. IPython: .error, .in-prompt, .out-prompt, etc |
|
71 | # 3. IPython: .error, .in-prompt, .out-prompt, etc | |
72 | style_sheet = Str(config=True) |
|
72 | style_sheet = Str(config=True) | |
73 |
|
73 | |||
74 | # If not empty, use this Pygments style for syntax highlighting. Otherwise, |
|
74 | # If not empty, use this Pygments style for syntax highlighting. Otherwise, | |
75 | # the style sheet is queried for Pygments style information. |
|
75 | # the style sheet is queried for Pygments style information. | |
76 | syntax_style = Str(config=True) |
|
76 | syntax_style = Str(config=True) | |
77 |
|
77 | |||
78 | # Prompts. |
|
78 | # Prompts. | |
79 | in_prompt = Str(default_in_prompt, config=True) |
|
79 | in_prompt = Str(default_in_prompt, config=True) | |
80 | out_prompt = Str(default_out_prompt, config=True) |
|
80 | out_prompt = Str(default_out_prompt, config=True) | |
81 | input_sep = Str(default_input_sep, config=True) |
|
81 | input_sep = Str(default_input_sep, config=True) | |
82 | output_sep = Str(default_output_sep, config=True) |
|
82 | output_sep = Str(default_output_sep, config=True) | |
83 | output_sep2 = Str(default_output_sep2, config=True) |
|
83 | output_sep2 = Str(default_output_sep2, config=True) | |
84 |
|
84 | |||
85 | # FrontendWidget protected class variables. |
|
85 | # FrontendWidget protected class variables. | |
86 | _input_splitter_class = IPythonInputSplitter |
|
86 | _input_splitter_class = IPythonInputSplitter | |
87 |
|
87 | |||
88 | # IPythonWidget protected class variables. |
|
88 | # IPythonWidget protected class variables. | |
89 | _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number']) |
|
89 | _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number']) | |
90 | _payload_source_edit = zmq_shell_source + '.edit_magic' |
|
90 | _payload_source_edit = zmq_shell_source + '.edit_magic' | |
91 | _payload_source_exit = zmq_shell_source + '.ask_exit' |
|
91 | _payload_source_exit = zmq_shell_source + '.ask_exit' | |
92 | _payload_source_loadpy = zmq_shell_source + '.magic_loadpy' |
|
92 | _payload_source_loadpy = zmq_shell_source + '.magic_loadpy' | |
93 | _payload_source_page = 'IPython.zmq.page.page' |
|
93 | _payload_source_page = 'IPython.zmq.page.page' | |
94 |
|
94 | |||
95 | #--------------------------------------------------------------------------- |
|
95 | #--------------------------------------------------------------------------- | |
96 | # 'object' interface |
|
96 | # 'object' interface | |
97 | #--------------------------------------------------------------------------- |
|
97 | #--------------------------------------------------------------------------- | |
98 |
|
98 | |||
99 | def __init__(self, *args, **kw): |
|
99 | def __init__(self, *args, **kw): | |
100 | super(IPythonWidget, self).__init__(*args, **kw) |
|
100 | super(IPythonWidget, self).__init__(*args, **kw) | |
101 |
|
101 | |||
102 | # IPythonWidget protected variables. |
|
102 | # IPythonWidget protected variables. | |
103 | self._code_to_load = None |
|
103 | self._code_to_load = None | |
104 | self._payload_handlers = { |
|
104 | self._payload_handlers = { | |
105 | self._payload_source_edit : self._handle_payload_edit, |
|
105 | self._payload_source_edit : self._handle_payload_edit, | |
106 | self._payload_source_exit : self._handle_payload_exit, |
|
106 | self._payload_source_exit : self._handle_payload_exit, | |
107 | self._payload_source_page : self._handle_payload_page, |
|
107 | self._payload_source_page : self._handle_payload_page, | |
108 | self._payload_source_loadpy : self._handle_payload_loadpy } |
|
108 | self._payload_source_loadpy : self._handle_payload_loadpy } | |
109 | self._previous_prompt_obj = None |
|
109 | self._previous_prompt_obj = None | |
110 | self._keep_kernel_on_exit = None |
|
110 | self._keep_kernel_on_exit = None | |
111 |
|
111 | |||
112 | # Initialize widget styling. |
|
112 | # Initialize widget styling. | |
113 | if self.style_sheet: |
|
113 | if self.style_sheet: | |
114 | self._style_sheet_changed() |
|
114 | self._style_sheet_changed() | |
115 | self._syntax_style_changed() |
|
115 | self._syntax_style_changed() | |
116 | else: |
|
116 | else: | |
117 | self.set_default_style() |
|
117 | self.set_default_style() | |
118 |
|
118 | |||
119 | #--------------------------------------------------------------------------- |
|
119 | #--------------------------------------------------------------------------- | |
120 | # 'BaseFrontendMixin' abstract interface |
|
120 | # 'BaseFrontendMixin' abstract interface | |
121 | #--------------------------------------------------------------------------- |
|
121 | #--------------------------------------------------------------------------- | |
122 |
|
122 | |||
123 | def _handle_complete_reply(self, rep): |
|
123 | def _handle_complete_reply(self, rep): | |
124 | """ Reimplemented to support IPython's improved completion machinery. |
|
124 | """ Reimplemented to support IPython's improved completion machinery. | |
125 | """ |
|
125 | """ | |
126 | cursor = self._get_cursor() |
|
126 | cursor = self._get_cursor() | |
127 | info = self._request_info.get('complete') |
|
127 | info = self._request_info.get('complete') | |
128 | if info and info.id == rep['parent_header']['msg_id'] and \ |
|
128 | if info and info.id == rep['parent_header']['msg_id'] and \ | |
129 | info.pos == cursor.position(): |
|
129 | info.pos == cursor.position(): | |
130 | matches = rep['content']['matches'] |
|
130 | matches = rep['content']['matches'] | |
131 | text = rep['content']['matched_text'] |
|
131 | text = rep['content']['matched_text'] | |
132 | offset = len(text) |
|
132 | offset = len(text) | |
133 |
|
133 | |||
134 | # Clean up matches with period and path separators if the matched |
|
134 | # Clean up matches with period and path separators if the matched | |
135 | # text has not been transformed. This is done by truncating all |
|
135 | # text has not been transformed. This is done by truncating all | |
136 | # but the last component and then suitably decreasing the offset |
|
136 | # but the last component and then suitably decreasing the offset | |
137 | # between the current cursor position and the start of completion. |
|
137 | # between the current cursor position and the start of completion. | |
138 | if len(matches) > 1 and matches[0][:offset] == text: |
|
138 | if len(matches) > 1 and matches[0][:offset] == text: | |
139 | parts = re.split(r'[./\\]', text) |
|
139 | parts = re.split(r'[./\\]', text) | |
140 | sep_count = len(parts) - 1 |
|
140 | sep_count = len(parts) - 1 | |
141 | if sep_count: |
|
141 | if sep_count: | |
142 | chop_length = sum(map(len, parts[:sep_count])) + sep_count |
|
142 | chop_length = sum(map(len, parts[:sep_count])) + sep_count | |
143 | matches = [ match[chop_length:] for match in matches ] |
|
143 | matches = [ match[chop_length:] for match in matches ] | |
144 | offset -= chop_length |
|
144 | offset -= chop_length | |
145 |
|
145 | |||
146 | # Move the cursor to the start of the match and complete. |
|
146 | # Move the cursor to the start of the match and complete. | |
147 | cursor.movePosition(QtGui.QTextCursor.Left, n=offset) |
|
147 | cursor.movePosition(QtGui.QTextCursor.Left, n=offset) | |
148 | self._complete_with_items(cursor, matches) |
|
148 | self._complete_with_items(cursor, matches) | |
149 |
|
149 | |||
150 | def _handle_execute_reply(self, msg): |
|
150 | def _handle_execute_reply(self, msg): | |
151 | """ Reimplemented to support prompt requests. |
|
151 | """ Reimplemented to support prompt requests. | |
152 | """ |
|
152 | """ | |
153 | info = self._request_info.get('execute') |
|
153 | info = self._request_info.get('execute') | |
154 | if info and info.id == msg['parent_header']['msg_id']: |
|
154 | if info and info.id == msg['parent_header']['msg_id']: | |
155 | if info.kind == 'prompt': |
|
155 | if info.kind == 'prompt': | |
156 | number = msg['content']['execution_count'] + 1 |
|
156 | number = msg['content']['execution_count'] + 1 | |
157 | self._show_interpreter_prompt(number) |
|
157 | self._show_interpreter_prompt(number) | |
158 | else: |
|
158 | else: | |
159 | super(IPythonWidget, self)._handle_execute_reply(msg) |
|
159 | super(IPythonWidget, self)._handle_execute_reply(msg) | |
160 |
|
160 | |||
161 | def _handle_history_reply(self, msg): |
|
161 | def _handle_history_tail_reply(self, msg): | |
162 |
""" Implemented to handle history replies, which are only supported |
|
162 | """ Implemented to handle history tail replies, which are only supported | |
163 | the IPython kernel. |
|
163 | by the IPython kernel. | |
164 | """ |
|
164 | """ | |
165 |
history_ |
|
165 | history_items = msg['content']['history'] | |
166 | input_history_dict = {} |
|
166 | items = [ line.rstrip() for _, _, line in history_items ] | |
167 | for key,val in history_dict.items(): |
|
|||
168 | input_history_dict[int(key)] = val |
|
|||
169 | items = [ val.rstrip() for _, val in sorted(input_history_dict.items()) ] |
|
|||
170 | self._set_history(items) |
|
167 | self._set_history(items) | |
171 |
|
168 | |||
172 | def _handle_pyout(self, msg): |
|
169 | def _handle_pyout(self, msg): | |
173 | """ Reimplemented for IPython-style "display hook". |
|
170 | """ Reimplemented for IPython-style "display hook". | |
174 | """ |
|
171 | """ | |
175 | if not self._hidden and self._is_from_this_session(msg): |
|
172 | if not self._hidden and self._is_from_this_session(msg): | |
176 | content = msg['content'] |
|
173 | content = msg['content'] | |
177 | prompt_number = content['execution_count'] |
|
174 | prompt_number = content['execution_count'] | |
178 | data = content['data'] |
|
175 | data = content['data'] | |
179 | if data.has_key('text/html'): |
|
176 | if data.has_key('text/html'): | |
180 | self._append_plain_text(self.output_sep) |
|
177 | self._append_plain_text(self.output_sep) | |
181 | self._append_html(self._make_out_prompt(prompt_number)) |
|
178 | self._append_html(self._make_out_prompt(prompt_number)) | |
182 | html = data['text/html'] |
|
179 | html = data['text/html'] | |
183 | self._append_plain_text('\n') |
|
180 | self._append_plain_text('\n') | |
184 | self._append_html(html + self.output_sep2) |
|
181 | self._append_html(html + self.output_sep2) | |
185 | elif data.has_key('text/plain'): |
|
182 | elif data.has_key('text/plain'): | |
186 | self._append_plain_text(self.output_sep) |
|
183 | self._append_plain_text(self.output_sep) | |
187 | self._append_html(self._make_out_prompt(prompt_number)) |
|
184 | self._append_html(self._make_out_prompt(prompt_number)) | |
188 | text = data['text/plain'] |
|
185 | text = data['text/plain'] | |
189 | self._append_plain_text(text + self.output_sep2) |
|
186 | self._append_plain_text(text + self.output_sep2) | |
190 |
|
187 | |||
191 | def _handle_display_data(self, msg): |
|
188 | def _handle_display_data(self, msg): | |
192 | """ The base handler for the ``display_data`` message. |
|
189 | """ The base handler for the ``display_data`` message. | |
193 | """ |
|
190 | """ | |
194 | # For now, we don't display data from other frontends, but we |
|
191 | # For now, we don't display data from other frontends, but we | |
195 | # eventually will as this allows all frontends to monitor the display |
|
192 | # eventually will as this allows all frontends to monitor the display | |
196 | # data. But we need to figure out how to handle this in the GUI. |
|
193 | # data. But we need to figure out how to handle this in the GUI. | |
197 | if not self._hidden and self._is_from_this_session(msg): |
|
194 | if not self._hidden and self._is_from_this_session(msg): | |
198 | source = msg['content']['source'] |
|
195 | source = msg['content']['source'] | |
199 | data = msg['content']['data'] |
|
196 | data = msg['content']['data'] | |
200 | metadata = msg['content']['metadata'] |
|
197 | metadata = msg['content']['metadata'] | |
201 | # In the regular IPythonWidget, we simply print the plain text |
|
198 | # In the regular IPythonWidget, we simply print the plain text | |
202 | # representation. |
|
199 | # representation. | |
203 | if data.has_key('text/html'): |
|
200 | if data.has_key('text/html'): | |
204 | html = data['text/html'] |
|
201 | html = data['text/html'] | |
205 | self._append_html(html) |
|
202 | self._append_html(html) | |
206 | elif data.has_key('text/plain'): |
|
203 | elif data.has_key('text/plain'): | |
207 | text = data['text/plain'] |
|
204 | text = data['text/plain'] | |
208 | self._append_plain_text(text) |
|
205 | self._append_plain_text(text) | |
209 | # This newline seems to be needed for text and html output. |
|
206 | # This newline seems to be needed for text and html output. | |
210 | self._append_plain_text(u'\n') |
|
207 | self._append_plain_text(u'\n') | |
211 |
|
208 | |||
212 | def _started_channels(self): |
|
209 | def _started_channels(self): | |
213 | """ Reimplemented to make a history request. |
|
210 | """ Reimplemented to make a history request. | |
214 | """ |
|
211 | """ | |
215 | super(IPythonWidget, self)._started_channels() |
|
212 | super(IPythonWidget, self)._started_channels() | |
216 |
self.kernel_manager.xreq_channel.history( |
|
213 | self.kernel_manager.xreq_channel.history_tail(1000) | |
217 | this_session=False) |
|
|||
218 |
|
214 | |||
219 | #--------------------------------------------------------------------------- |
|
215 | #--------------------------------------------------------------------------- | |
220 | # 'ConsoleWidget' public interface |
|
216 | # 'ConsoleWidget' public interface | |
221 | #--------------------------------------------------------------------------- |
|
217 | #--------------------------------------------------------------------------- | |
222 |
|
218 | |||
223 | def copy(self): |
|
219 | def copy(self): | |
224 | """ Copy the currently selected text to the clipboard, removing prompts |
|
220 | """ Copy the currently selected text to the clipboard, removing prompts | |
225 | if possible. |
|
221 | if possible. | |
226 | """ |
|
222 | """ | |
227 | text = self._control.textCursor().selection().toPlainText() |
|
223 | text = self._control.textCursor().selection().toPlainText() | |
228 | if text: |
|
224 | if text: | |
229 | lines = map(transform_ipy_prompt, text.splitlines()) |
|
225 | lines = map(transform_ipy_prompt, text.splitlines()) | |
230 | text = '\n'.join(lines) |
|
226 | text = '\n'.join(lines) | |
231 | QtGui.QApplication.clipboard().setText(text) |
|
227 | QtGui.QApplication.clipboard().setText(text) | |
232 |
|
228 | |||
233 | #--------------------------------------------------------------------------- |
|
229 | #--------------------------------------------------------------------------- | |
234 | # 'FrontendWidget' public interface |
|
230 | # 'FrontendWidget' public interface | |
235 | #--------------------------------------------------------------------------- |
|
231 | #--------------------------------------------------------------------------- | |
236 |
|
232 | |||
237 | def execute_file(self, path, hidden=False): |
|
233 | def execute_file(self, path, hidden=False): | |
238 | """ Reimplemented to use the 'run' magic. |
|
234 | """ Reimplemented to use the 'run' magic. | |
239 | """ |
|
235 | """ | |
240 | self.execute('%%run %s' % path, hidden=hidden) |
|
236 | self.execute('%%run %s' % path, hidden=hidden) | |
241 |
|
237 | |||
242 | #--------------------------------------------------------------------------- |
|
238 | #--------------------------------------------------------------------------- | |
243 | # 'FrontendWidget' protected interface |
|
239 | # 'FrontendWidget' protected interface | |
244 | #--------------------------------------------------------------------------- |
|
240 | #--------------------------------------------------------------------------- | |
245 |
|
241 | |||
246 | def _complete(self): |
|
242 | def _complete(self): | |
247 | """ Reimplemented to support IPython's improved completion machinery. |
|
243 | """ Reimplemented to support IPython's improved completion machinery. | |
248 | """ |
|
244 | """ | |
249 | # We let the kernel split the input line, so we *always* send an empty |
|
245 | # We let the kernel split the input line, so we *always* send an empty | |
250 | # text field. Readline-based frontends do get a real text field which |
|
246 | # text field. Readline-based frontends do get a real text field which | |
251 | # they can use. |
|
247 | # they can use. | |
252 | text = '' |
|
248 | text = '' | |
253 |
|
249 | |||
254 | # Send the completion request to the kernel |
|
250 | # Send the completion request to the kernel | |
255 | msg_id = self.kernel_manager.xreq_channel.complete( |
|
251 | msg_id = self.kernel_manager.xreq_channel.complete( | |
256 | text, # text |
|
252 | text, # text | |
257 | self._get_input_buffer_cursor_line(), # line |
|
253 | self._get_input_buffer_cursor_line(), # line | |
258 | self._get_input_buffer_cursor_column(), # cursor_pos |
|
254 | self._get_input_buffer_cursor_column(), # cursor_pos | |
259 | self.input_buffer) # block |
|
255 | self.input_buffer) # block | |
260 | pos = self._get_cursor().position() |
|
256 | pos = self._get_cursor().position() | |
261 | info = self._CompletionRequest(msg_id, pos) |
|
257 | info = self._CompletionRequest(msg_id, pos) | |
262 | self._request_info['complete'] = info |
|
258 | self._request_info['complete'] = info | |
263 |
|
259 | |||
264 | def _get_banner(self): |
|
260 | def _get_banner(self): | |
265 | """ Reimplemented to return IPython's default banner. |
|
261 | """ Reimplemented to return IPython's default banner. | |
266 | """ |
|
262 | """ | |
267 | return default_gui_banner |
|
263 | return default_gui_banner | |
268 |
|
264 | |||
269 | def _process_execute_error(self, msg): |
|
265 | def _process_execute_error(self, msg): | |
270 | """ Reimplemented for IPython-style traceback formatting. |
|
266 | """ Reimplemented for IPython-style traceback formatting. | |
271 | """ |
|
267 | """ | |
272 | content = msg['content'] |
|
268 | content = msg['content'] | |
273 | traceback = '\n'.join(content['traceback']) + '\n' |
|
269 | traceback = '\n'.join(content['traceback']) + '\n' | |
274 | if False: |
|
270 | if False: | |
275 | # FIXME: For now, tracebacks come as plain text, so we can't use |
|
271 | # FIXME: For now, tracebacks come as plain text, so we can't use | |
276 | # the html renderer yet. Once we refactor ultratb to produce |
|
272 | # the html renderer yet. Once we refactor ultratb to produce | |
277 | # properly styled tracebacks, this branch should be the default |
|
273 | # properly styled tracebacks, this branch should be the default | |
278 | traceback = traceback.replace(' ', ' ') |
|
274 | traceback = traceback.replace(' ', ' ') | |
279 | traceback = traceback.replace('\n', '<br/>') |
|
275 | traceback = traceback.replace('\n', '<br/>') | |
280 |
|
276 | |||
281 | ename = content['ename'] |
|
277 | ename = content['ename'] | |
282 | ename_styled = '<span class="error">%s</span>' % ename |
|
278 | ename_styled = '<span class="error">%s</span>' % ename | |
283 | traceback = traceback.replace(ename, ename_styled) |
|
279 | traceback = traceback.replace(ename, ename_styled) | |
284 |
|
280 | |||
285 | self._append_html(traceback) |
|
281 | self._append_html(traceback) | |
286 | else: |
|
282 | else: | |
287 | # This is the fallback for now, using plain text with ansi escapes |
|
283 | # This is the fallback for now, using plain text with ansi escapes | |
288 | self._append_plain_text(traceback) |
|
284 | self._append_plain_text(traceback) | |
289 |
|
285 | |||
290 | def _process_execute_payload(self, item): |
|
286 | def _process_execute_payload(self, item): | |
291 | """ Reimplemented to dispatch payloads to handler methods. |
|
287 | """ Reimplemented to dispatch payloads to handler methods. | |
292 | """ |
|
288 | """ | |
293 | handler = self._payload_handlers.get(item['source']) |
|
289 | handler = self._payload_handlers.get(item['source']) | |
294 | if handler is None: |
|
290 | if handler is None: | |
295 | # We have no handler for this type of payload, simply ignore it |
|
291 | # We have no handler for this type of payload, simply ignore it | |
296 | return False |
|
292 | return False | |
297 | else: |
|
293 | else: | |
298 | handler(item) |
|
294 | handler(item) | |
299 | return True |
|
295 | return True | |
300 |
|
296 | |||
301 | def _show_interpreter_prompt(self, number=None): |
|
297 | def _show_interpreter_prompt(self, number=None): | |
302 | """ Reimplemented for IPython-style prompts. |
|
298 | """ Reimplemented for IPython-style prompts. | |
303 | """ |
|
299 | """ | |
304 | # If a number was not specified, make a prompt number request. |
|
300 | # If a number was not specified, make a prompt number request. | |
305 | if number is None: |
|
301 | if number is None: | |
306 | msg_id = self.kernel_manager.xreq_channel.execute('', silent=True) |
|
302 | msg_id = self.kernel_manager.xreq_channel.execute('', silent=True) | |
307 | info = self._ExecutionRequest(msg_id, 'prompt') |
|
303 | info = self._ExecutionRequest(msg_id, 'prompt') | |
308 | self._request_info['execute'] = info |
|
304 | self._request_info['execute'] = info | |
309 | return |
|
305 | return | |
310 |
|
306 | |||
311 | # Show a new prompt and save information about it so that it can be |
|
307 | # Show a new prompt and save information about it so that it can be | |
312 | # updated later if the prompt number turns out to be wrong. |
|
308 | # updated later if the prompt number turns out to be wrong. | |
313 | self._prompt_sep = self.input_sep |
|
309 | self._prompt_sep = self.input_sep | |
314 | self._show_prompt(self._make_in_prompt(number), html=True) |
|
310 | self._show_prompt(self._make_in_prompt(number), html=True) | |
315 | block = self._control.document().lastBlock() |
|
311 | block = self._control.document().lastBlock() | |
316 | length = len(self._prompt) |
|
312 | length = len(self._prompt) | |
317 | self._previous_prompt_obj = self._PromptBlock(block, length, number) |
|
313 | self._previous_prompt_obj = self._PromptBlock(block, length, number) | |
318 |
|
314 | |||
319 | # Update continuation prompt to reflect (possibly) new prompt length. |
|
315 | # Update continuation prompt to reflect (possibly) new prompt length. | |
320 | self._set_continuation_prompt( |
|
316 | self._set_continuation_prompt( | |
321 | self._make_continuation_prompt(self._prompt), html=True) |
|
317 | self._make_continuation_prompt(self._prompt), html=True) | |
322 |
|
318 | |||
323 | # Load code from the %loadpy magic, if necessary. |
|
319 | # Load code from the %loadpy magic, if necessary. | |
324 | if self._code_to_load is not None: |
|
320 | if self._code_to_load is not None: | |
325 | self.input_buffer = dedent(self._code_to_load.rstrip()) |
|
321 | self.input_buffer = dedent(self._code_to_load.rstrip()) | |
326 | self._code_to_load = None |
|
322 | self._code_to_load = None | |
327 |
|
323 | |||
328 | def _show_interpreter_prompt_for_reply(self, msg): |
|
324 | def _show_interpreter_prompt_for_reply(self, msg): | |
329 | """ Reimplemented for IPython-style prompts. |
|
325 | """ Reimplemented for IPython-style prompts. | |
330 | """ |
|
326 | """ | |
331 | # Update the old prompt number if necessary. |
|
327 | # Update the old prompt number if necessary. | |
332 | content = msg['content'] |
|
328 | content = msg['content'] | |
333 | previous_prompt_number = content['execution_count'] |
|
329 | previous_prompt_number = content['execution_count'] | |
334 | if self._previous_prompt_obj and \ |
|
330 | if self._previous_prompt_obj and \ | |
335 | self._previous_prompt_obj.number != previous_prompt_number: |
|
331 | self._previous_prompt_obj.number != previous_prompt_number: | |
336 | block = self._previous_prompt_obj.block |
|
332 | block = self._previous_prompt_obj.block | |
337 |
|
333 | |||
338 | # Make sure the prompt block has not been erased. |
|
334 | # Make sure the prompt block has not been erased. | |
339 | if block.isValid() and block.text(): |
|
335 | if block.isValid() and block.text(): | |
340 |
|
336 | |||
341 | # Remove the old prompt and insert a new prompt. |
|
337 | # Remove the old prompt and insert a new prompt. | |
342 | cursor = QtGui.QTextCursor(block) |
|
338 | cursor = QtGui.QTextCursor(block) | |
343 | cursor.movePosition(QtGui.QTextCursor.Right, |
|
339 | cursor.movePosition(QtGui.QTextCursor.Right, | |
344 | QtGui.QTextCursor.KeepAnchor, |
|
340 | QtGui.QTextCursor.KeepAnchor, | |
345 | self._previous_prompt_obj.length) |
|
341 | self._previous_prompt_obj.length) | |
346 | prompt = self._make_in_prompt(previous_prompt_number) |
|
342 | prompt = self._make_in_prompt(previous_prompt_number) | |
347 | self._prompt = self._insert_html_fetching_plain_text( |
|
343 | self._prompt = self._insert_html_fetching_plain_text( | |
348 | cursor, prompt) |
|
344 | cursor, prompt) | |
349 |
|
345 | |||
350 | # When the HTML is inserted, Qt blows away the syntax |
|
346 | # When the HTML is inserted, Qt blows away the syntax | |
351 | # highlighting for the line, so we need to rehighlight it. |
|
347 | # highlighting for the line, so we need to rehighlight it. | |
352 | self._highlighter.rehighlightBlock(cursor.block()) |
|
348 | self._highlighter.rehighlightBlock(cursor.block()) | |
353 |
|
349 | |||
354 | self._previous_prompt_obj = None |
|
350 | self._previous_prompt_obj = None | |
355 |
|
351 | |||
356 | # Show a new prompt with the kernel's estimated prompt number. |
|
352 | # Show a new prompt with the kernel's estimated prompt number. | |
357 | self._show_interpreter_prompt(previous_prompt_number + 1) |
|
353 | self._show_interpreter_prompt(previous_prompt_number + 1) | |
358 |
|
354 | |||
359 | #--------------------------------------------------------------------------- |
|
355 | #--------------------------------------------------------------------------- | |
360 | # 'IPythonWidget' interface |
|
356 | # 'IPythonWidget' interface | |
361 | #--------------------------------------------------------------------------- |
|
357 | #--------------------------------------------------------------------------- | |
362 |
|
358 | |||
363 | def set_default_style(self, colors='lightbg'): |
|
359 | def set_default_style(self, colors='lightbg'): | |
364 | """ Sets the widget style to the class defaults. |
|
360 | """ Sets the widget style to the class defaults. | |
365 |
|
361 | |||
366 | Parameters: |
|
362 | Parameters: | |
367 | ----------- |
|
363 | ----------- | |
368 | colors : str, optional (default lightbg) |
|
364 | colors : str, optional (default lightbg) | |
369 | Whether to use the default IPython light background or dark |
|
365 | Whether to use the default IPython light background or dark | |
370 | background or B&W style. |
|
366 | background or B&W style. | |
371 | """ |
|
367 | """ | |
372 | colors = colors.lower() |
|
368 | colors = colors.lower() | |
373 | if colors=='lightbg': |
|
369 | if colors=='lightbg': | |
374 | self.style_sheet = default_light_style_sheet |
|
370 | self.style_sheet = default_light_style_sheet | |
375 | self.syntax_style = default_light_syntax_style |
|
371 | self.syntax_style = default_light_syntax_style | |
376 | elif colors=='linux': |
|
372 | elif colors=='linux': | |
377 | self.style_sheet = default_dark_style_sheet |
|
373 | self.style_sheet = default_dark_style_sheet | |
378 | self.syntax_style = default_dark_syntax_style |
|
374 | self.syntax_style = default_dark_syntax_style | |
379 | elif colors=='nocolor': |
|
375 | elif colors=='nocolor': | |
380 | self.style_sheet = default_bw_style_sheet |
|
376 | self.style_sheet = default_bw_style_sheet | |
381 | self.syntax_style = default_bw_syntax_style |
|
377 | self.syntax_style = default_bw_syntax_style | |
382 | else: |
|
378 | else: | |
383 | raise KeyError("No such color scheme: %s"%colors) |
|
379 | raise KeyError("No such color scheme: %s"%colors) | |
384 |
|
380 | |||
385 | #--------------------------------------------------------------------------- |
|
381 | #--------------------------------------------------------------------------- | |
386 | # 'IPythonWidget' protected interface |
|
382 | # 'IPythonWidget' protected interface | |
387 | #--------------------------------------------------------------------------- |
|
383 | #--------------------------------------------------------------------------- | |
388 |
|
384 | |||
389 | def _edit(self, filename, line=None): |
|
385 | def _edit(self, filename, line=None): | |
390 | """ Opens a Python script for editing. |
|
386 | """ Opens a Python script for editing. | |
391 |
|
387 | |||
392 | Parameters: |
|
388 | Parameters: | |
393 | ----------- |
|
389 | ----------- | |
394 | filename : str |
|
390 | filename : str | |
395 | A path to a local system file. |
|
391 | A path to a local system file. | |
396 |
|
392 | |||
397 | line : int, optional |
|
393 | line : int, optional | |
398 | A line of interest in the file. |
|
394 | A line of interest in the file. | |
399 | """ |
|
395 | """ | |
400 | if self.custom_edit: |
|
396 | if self.custom_edit: | |
401 | self.custom_edit_requested.emit(filename, line) |
|
397 | self.custom_edit_requested.emit(filename, line) | |
402 | elif self.editor == 'default': |
|
398 | elif self.editor == 'default': | |
403 | self._append_plain_text('No default editor available.\n') |
|
399 | self._append_plain_text('No default editor available.\n') | |
404 | else: |
|
400 | else: | |
405 | try: |
|
401 | try: | |
406 | filename = '"%s"' % filename |
|
402 | filename = '"%s"' % filename | |
407 | if line and self.editor_line: |
|
403 | if line and self.editor_line: | |
408 | command = self.editor_line.format(filename=filename, |
|
404 | command = self.editor_line.format(filename=filename, | |
409 | line=line) |
|
405 | line=line) | |
410 | else: |
|
406 | else: | |
411 | try: |
|
407 | try: | |
412 | command = self.editor.format() |
|
408 | command = self.editor.format() | |
413 | except KeyError: |
|
409 | except KeyError: | |
414 | command = self.editor.format(filename=filename) |
|
410 | command = self.editor.format(filename=filename) | |
415 | else: |
|
411 | else: | |
416 | command += ' ' + filename |
|
412 | command += ' ' + filename | |
417 | except KeyError: |
|
413 | except KeyError: | |
418 | self._append_plain_text('Invalid editor command.\n') |
|
414 | self._append_plain_text('Invalid editor command.\n') | |
419 | else: |
|
415 | else: | |
420 | try: |
|
416 | try: | |
421 | Popen(command, shell=True) |
|
417 | Popen(command, shell=True) | |
422 | except OSError: |
|
418 | except OSError: | |
423 | msg = 'Opening editor with command "%s" failed.\n' |
|
419 | msg = 'Opening editor with command "%s" failed.\n' | |
424 | self._append_plain_text(msg % command) |
|
420 | self._append_plain_text(msg % command) | |
425 |
|
421 | |||
426 | def _make_in_prompt(self, number): |
|
422 | def _make_in_prompt(self, number): | |
427 | """ Given a prompt number, returns an HTML In prompt. |
|
423 | """ Given a prompt number, returns an HTML In prompt. | |
428 | """ |
|
424 | """ | |
429 | body = self.in_prompt % number |
|
425 | body = self.in_prompt % number | |
430 | return '<span class="in-prompt">%s</span>' % body |
|
426 | return '<span class="in-prompt">%s</span>' % body | |
431 |
|
427 | |||
432 | def _make_continuation_prompt(self, prompt): |
|
428 | def _make_continuation_prompt(self, prompt): | |
433 | """ Given a plain text version of an In prompt, returns an HTML |
|
429 | """ Given a plain text version of an In prompt, returns an HTML | |
434 | continuation prompt. |
|
430 | continuation prompt. | |
435 | """ |
|
431 | """ | |
436 | end_chars = '...: ' |
|
432 | end_chars = '...: ' | |
437 | space_count = len(prompt.lstrip('\n')) - len(end_chars) |
|
433 | space_count = len(prompt.lstrip('\n')) - len(end_chars) | |
438 | body = ' ' * space_count + end_chars |
|
434 | body = ' ' * space_count + end_chars | |
439 | return '<span class="in-prompt">%s</span>' % body |
|
435 | return '<span class="in-prompt">%s</span>' % body | |
440 |
|
436 | |||
441 | def _make_out_prompt(self, number): |
|
437 | def _make_out_prompt(self, number): | |
442 | """ Given a prompt number, returns an HTML Out prompt. |
|
438 | """ Given a prompt number, returns an HTML Out prompt. | |
443 | """ |
|
439 | """ | |
444 | body = self.out_prompt % number |
|
440 | body = self.out_prompt % number | |
445 | return '<span class="out-prompt">%s</span>' % body |
|
441 | return '<span class="out-prompt">%s</span>' % body | |
446 |
|
442 | |||
447 | #------ Payload handlers -------------------------------------------------- |
|
443 | #------ Payload handlers -------------------------------------------------- | |
448 |
|
444 | |||
449 | # Payload handlers with a generic interface: each takes the opaque payload |
|
445 | # Payload handlers with a generic interface: each takes the opaque payload | |
450 | # dict, unpacks it and calls the underlying functions with the necessary |
|
446 | # dict, unpacks it and calls the underlying functions with the necessary | |
451 | # arguments. |
|
447 | # arguments. | |
452 |
|
448 | |||
453 | def _handle_payload_edit(self, item): |
|
449 | def _handle_payload_edit(self, item): | |
454 | self._edit(item['filename'], item['line_number']) |
|
450 | self._edit(item['filename'], item['line_number']) | |
455 |
|
451 | |||
456 | def _handle_payload_exit(self, item): |
|
452 | def _handle_payload_exit(self, item): | |
457 | self._keep_kernel_on_exit = item['keepkernel'] |
|
453 | self._keep_kernel_on_exit = item['keepkernel'] | |
458 | self.exit_requested.emit() |
|
454 | self.exit_requested.emit() | |
459 |
|
455 | |||
460 | def _handle_payload_loadpy(self, item): |
|
456 | def _handle_payload_loadpy(self, item): | |
461 | # Simple save the text of the .py file for later. The text is written |
|
457 | # Simple save the text of the .py file for later. The text is written | |
462 | # to the buffer when _prompt_started_hook is called. |
|
458 | # to the buffer when _prompt_started_hook is called. | |
463 | self._code_to_load = item['text'] |
|
459 | self._code_to_load = item['text'] | |
464 |
|
460 | |||
465 | def _handle_payload_page(self, item): |
|
461 | def _handle_payload_page(self, item): | |
466 | # Since the plain text widget supports only a very small subset of HTML |
|
462 | # Since the plain text widget supports only a very small subset of HTML | |
467 | # and we have no control over the HTML source, we only page HTML |
|
463 | # and we have no control over the HTML source, we only page HTML | |
468 | # payloads in the rich text widget. |
|
464 | # payloads in the rich text widget. | |
469 | if item['html'] and self.kind == 'rich': |
|
465 | if item['html'] and self.kind == 'rich': | |
470 | self._page(item['html'], html=True) |
|
466 | self._page(item['html'], html=True) | |
471 | else: |
|
467 | else: | |
472 | self._page(item['text'], html=False) |
|
468 | self._page(item['text'], html=False) | |
473 |
|
469 | |||
474 | #------ Trait change handlers -------------------------------------------- |
|
470 | #------ Trait change handlers -------------------------------------------- | |
475 |
|
471 | |||
476 | def _style_sheet_changed(self): |
|
472 | def _style_sheet_changed(self): | |
477 | """ Set the style sheets of the underlying widgets. |
|
473 | """ Set the style sheets of the underlying widgets. | |
478 | """ |
|
474 | """ | |
479 | self.setStyleSheet(self.style_sheet) |
|
475 | self.setStyleSheet(self.style_sheet) | |
480 | self._control.document().setDefaultStyleSheet(self.style_sheet) |
|
476 | self._control.document().setDefaultStyleSheet(self.style_sheet) | |
481 | if self._page_control: |
|
477 | if self._page_control: | |
482 | self._page_control.document().setDefaultStyleSheet(self.style_sheet) |
|
478 | self._page_control.document().setDefaultStyleSheet(self.style_sheet) | |
483 |
|
479 | |||
484 | bg_color = self._control.palette().window().color() |
|
480 | bg_color = self._control.palette().window().color() | |
485 | self._ansi_processor.set_background_color(bg_color) |
|
481 | self._ansi_processor.set_background_color(bg_color) | |
486 |
|
482 | |||
487 | def _syntax_style_changed(self): |
|
483 | def _syntax_style_changed(self): | |
488 | """ Set the style for the syntax highlighter. |
|
484 | """ Set the style for the syntax highlighter. | |
489 | """ |
|
485 | """ | |
490 | if self.syntax_style: |
|
486 | if self.syntax_style: | |
491 | self._highlighter.set_style(self.syntax_style) |
|
487 | self._highlighter.set_style(self.syntax_style) | |
492 | else: |
|
488 | else: | |
493 | self._highlighter.set_style_sheet(self.style_sheet) |
|
489 | self._highlighter.set_style_sheet(self.style_sheet) | |
494 |
|
490 |
@@ -1,640 +1,639 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """A simple interactive kernel that talks to a frontend over 0MQ. |
|
2 | """A simple interactive kernel that talks to a frontend over 0MQ. | |
3 |
|
3 | |||
4 | Things to do: |
|
4 | Things to do: | |
5 |
|
5 | |||
6 | * Implement `set_parent` logic. Right before doing exec, the Kernel should |
|
6 | * Implement `set_parent` logic. Right before doing exec, the Kernel should | |
7 | call set_parent on all the PUB objects with the message about to be executed. |
|
7 | call set_parent on all the PUB objects with the message about to be executed. | |
8 | * Implement random port and security key logic. |
|
8 | * Implement random port and security key logic. | |
9 | * Implement control messages. |
|
9 | * Implement control messages. | |
10 | * Implement event loop and poll version. |
|
10 | * Implement event loop and poll version. | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from __future__ import print_function |
|
16 | from __future__ import print_function | |
17 |
|
17 | |||
18 | # Standard library imports. |
|
18 | # Standard library imports. | |
19 | import __builtin__ |
|
19 | import __builtin__ | |
20 | import atexit |
|
20 | import atexit | |
21 | import sys |
|
21 | import sys | |
22 | import time |
|
22 | import time | |
23 | import traceback |
|
23 | import traceback | |
24 | import logging |
|
24 | import logging | |
25 | # System library imports. |
|
25 | # System library imports. | |
26 | import zmq |
|
26 | import zmq | |
27 |
|
27 | |||
28 | # Local imports. |
|
28 | # Local imports. | |
29 | from IPython.config.configurable import Configurable |
|
29 | from IPython.config.configurable import Configurable | |
30 | from IPython.utils import io |
|
30 | from IPython.utils import io | |
31 | from IPython.utils.jsonutil import json_clean |
|
31 | from IPython.utils.jsonutil import json_clean | |
32 | from IPython.lib import pylabtools |
|
32 | from IPython.lib import pylabtools | |
33 | from IPython.utils.traitlets import Instance, Float |
|
33 | from IPython.utils.traitlets import Instance, Float | |
34 | from entry_point import (base_launch_kernel, make_argument_parser, make_kernel, |
|
34 | from entry_point import (base_launch_kernel, make_argument_parser, make_kernel, | |
35 | start_kernel) |
|
35 | start_kernel) | |
36 | from iostream import OutStream |
|
36 | from iostream import OutStream | |
37 | from session import Session, Message |
|
37 | from session import Session, Message | |
38 | from zmqshell import ZMQInteractiveShell |
|
38 | from zmqshell import ZMQInteractiveShell | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Globals |
|
41 | # Globals | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | # Module-level logger |
|
44 | # Module-level logger | |
45 | logger = logging.getLogger(__name__) |
|
45 | logger = logging.getLogger(__name__) | |
46 |
|
46 | |||
47 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
48 | # Main kernel class |
|
48 | # Main kernel class | |
49 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
50 |
|
50 | |||
51 | class Kernel(Configurable): |
|
51 | class Kernel(Configurable): | |
52 |
|
52 | |||
53 | #--------------------------------------------------------------------------- |
|
53 | #--------------------------------------------------------------------------- | |
54 | # Kernel interface |
|
54 | # Kernel interface | |
55 | #--------------------------------------------------------------------------- |
|
55 | #--------------------------------------------------------------------------- | |
56 |
|
56 | |||
57 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
57 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
58 | session = Instance(Session) |
|
58 | session = Instance(Session) | |
59 | reply_socket = Instance('zmq.Socket') |
|
59 | reply_socket = Instance('zmq.Socket') | |
60 | pub_socket = Instance('zmq.Socket') |
|
60 | pub_socket = Instance('zmq.Socket') | |
61 | req_socket = Instance('zmq.Socket') |
|
61 | req_socket = Instance('zmq.Socket') | |
62 |
|
62 | |||
63 | # Private interface |
|
63 | # Private interface | |
64 |
|
64 | |||
65 | # Time to sleep after flushing the stdout/err buffers in each execute |
|
65 | # Time to sleep after flushing the stdout/err buffers in each execute | |
66 | # cycle. While this introduces a hard limit on the minimal latency of the |
|
66 | # cycle. While this introduces a hard limit on the minimal latency of the | |
67 | # execute cycle, it helps prevent output synchronization problems for |
|
67 | # execute cycle, it helps prevent output synchronization problems for | |
68 | # clients. |
|
68 | # clients. | |
69 | # Units are in seconds. The minimum zmq latency on local host is probably |
|
69 | # Units are in seconds. The minimum zmq latency on local host is probably | |
70 | # ~150 microseconds, set this to 500us for now. We may need to increase it |
|
70 | # ~150 microseconds, set this to 500us for now. We may need to increase it | |
71 | # a little if it's not enough after more interactive testing. |
|
71 | # a little if it's not enough after more interactive testing. | |
72 | _execute_sleep = Float(0.0005, config=True) |
|
72 | _execute_sleep = Float(0.0005, config=True) | |
73 |
|
73 | |||
74 | # Frequency of the kernel's event loop. |
|
74 | # Frequency of the kernel's event loop. | |
75 | # Units are in seconds, kernel subclasses for GUI toolkits may need to |
|
75 | # Units are in seconds, kernel subclasses for GUI toolkits may need to | |
76 | # adapt to milliseconds. |
|
76 | # adapt to milliseconds. | |
77 | _poll_interval = Float(0.05, config=True) |
|
77 | _poll_interval = Float(0.05, config=True) | |
78 |
|
78 | |||
79 | # If the shutdown was requested over the network, we leave here the |
|
79 | # If the shutdown was requested over the network, we leave here the | |
80 | # necessary reply message so it can be sent by our registered atexit |
|
80 | # necessary reply message so it can be sent by our registered atexit | |
81 | # handler. This ensures that the reply is only sent to clients truly at |
|
81 | # handler. This ensures that the reply is only sent to clients truly at | |
82 | # the end of our shutdown process (which happens after the underlying |
|
82 | # the end of our shutdown process (which happens after the underlying | |
83 | # IPython shell's own shutdown). |
|
83 | # IPython shell's own shutdown). | |
84 | _shutdown_message = None |
|
84 | _shutdown_message = None | |
85 |
|
85 | |||
86 | # This is a dict of port number that the kernel is listening on. It is set |
|
86 | # This is a dict of port number that the kernel is listening on. It is set | |
87 | # by record_ports and used by connect_request. |
|
87 | # by record_ports and used by connect_request. | |
88 | _recorded_ports = None |
|
88 | _recorded_ports = None | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def __init__(self, **kwargs): |
|
91 | def __init__(self, **kwargs): | |
92 | super(Kernel, self).__init__(**kwargs) |
|
92 | super(Kernel, self).__init__(**kwargs) | |
93 |
|
93 | |||
94 | # Before we even start up the shell, register *first* our exit handlers |
|
94 | # Before we even start up the shell, register *first* our exit handlers | |
95 | # so they come before the shell's |
|
95 | # so they come before the shell's | |
96 | atexit.register(self._at_shutdown) |
|
96 | atexit.register(self._at_shutdown) | |
97 |
|
97 | |||
98 | # Initialize the InteractiveShell subclass |
|
98 | # Initialize the InteractiveShell subclass | |
99 | self.shell = ZMQInteractiveShell.instance() |
|
99 | self.shell = ZMQInteractiveShell.instance() | |
100 | self.shell.displayhook.session = self.session |
|
100 | self.shell.displayhook.session = self.session | |
101 | self.shell.displayhook.pub_socket = self.pub_socket |
|
101 | self.shell.displayhook.pub_socket = self.pub_socket | |
102 | self.shell.display_pub.session = self.session |
|
102 | self.shell.display_pub.session = self.session | |
103 | self.shell.display_pub.pub_socket = self.pub_socket |
|
103 | self.shell.display_pub.pub_socket = self.pub_socket | |
104 |
|
104 | |||
105 | # TMP - hack while developing |
|
105 | # TMP - hack while developing | |
106 | self.shell._reply_content = None |
|
106 | self.shell._reply_content = None | |
107 |
|
107 | |||
108 | # Build dict of handlers for message types |
|
108 | # Build dict of handlers for message types | |
109 | msg_types = [ 'execute_request', 'complete_request', |
|
109 | msg_types = [ 'execute_request', 'complete_request', | |
110 | 'object_info_request', 'history_request', |
|
110 | 'object_info_request', 'history_tail_request', | |
111 | 'connect_request', 'shutdown_request'] |
|
111 | 'connect_request', 'shutdown_request'] | |
112 | self.handlers = {} |
|
112 | self.handlers = {} | |
113 | for msg_type in msg_types: |
|
113 | for msg_type in msg_types: | |
114 | self.handlers[msg_type] = getattr(self, msg_type) |
|
114 | self.handlers[msg_type] = getattr(self, msg_type) | |
115 |
|
115 | |||
116 | def do_one_iteration(self): |
|
116 | def do_one_iteration(self): | |
117 | """Do one iteration of the kernel's evaluation loop. |
|
117 | """Do one iteration of the kernel's evaluation loop. | |
118 | """ |
|
118 | """ | |
119 | ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK) |
|
119 | ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK) | |
120 | if msg is None: |
|
120 | if msg is None: | |
121 | return |
|
121 | return | |
122 |
|
122 | |||
123 | # This assert will raise in versions of zeromq 2.0.7 and lesser. |
|
123 | # This assert will raise in versions of zeromq 2.0.7 and lesser. | |
124 | # We now require 2.0.8 or above, so we can uncomment for safety. |
|
124 | # We now require 2.0.8 or above, so we can uncomment for safety. | |
125 | # print(ident,msg, file=sys.__stdout__) |
|
125 | # print(ident,msg, file=sys.__stdout__) | |
126 | assert ident is not None, "Missing message part." |
|
126 | assert ident is not None, "Missing message part." | |
127 |
|
127 | |||
128 | # Print some info about this message and leave a '--->' marker, so it's |
|
128 | # Print some info about this message and leave a '--->' marker, so it's | |
129 | # easier to trace visually the message chain when debugging. Each |
|
129 | # easier to trace visually the message chain when debugging. Each | |
130 | # handler prints its message at the end. |
|
130 | # handler prints its message at the end. | |
131 | # Eventually we'll move these from stdout to a logger. |
|
131 | # Eventually we'll move these from stdout to a logger. | |
132 | logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***') |
|
132 | logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***') | |
133 | logger.debug(' Content: '+str(msg['content'])+'\n --->\n ') |
|
133 | logger.debug(' Content: '+str(msg['content'])+'\n --->\n ') | |
134 |
|
134 | |||
135 | # Find and call actual handler for message |
|
135 | # Find and call actual handler for message | |
136 | handler = self.handlers.get(msg['msg_type'], None) |
|
136 | handler = self.handlers.get(msg['msg_type'], None) | |
137 | if handler is None: |
|
137 | if handler is None: | |
138 | logger.error("UNKNOWN MESSAGE TYPE:" +str(msg)) |
|
138 | logger.error("UNKNOWN MESSAGE TYPE:" +str(msg)) | |
139 | else: |
|
139 | else: | |
140 | handler(ident, msg) |
|
140 | handler(ident, msg) | |
141 |
|
141 | |||
142 | # Check whether we should exit, in case the incoming message set the |
|
142 | # Check whether we should exit, in case the incoming message set the | |
143 | # exit flag on |
|
143 | # exit flag on | |
144 | if self.shell.exit_now: |
|
144 | if self.shell.exit_now: | |
145 | logger.debug('\nExiting IPython kernel...') |
|
145 | logger.debug('\nExiting IPython kernel...') | |
146 | # We do a normal, clean exit, which allows any actions registered |
|
146 | # We do a normal, clean exit, which allows any actions registered | |
147 | # via atexit (such as history saving) to take place. |
|
147 | # via atexit (such as history saving) to take place. | |
148 | sys.exit(0) |
|
148 | sys.exit(0) | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | def start(self): |
|
151 | def start(self): | |
152 | """ Start the kernel main loop. |
|
152 | """ Start the kernel main loop. | |
153 | """ |
|
153 | """ | |
154 | while True: |
|
154 | while True: | |
155 | time.sleep(self._poll_interval) |
|
155 | time.sleep(self._poll_interval) | |
156 | self.do_one_iteration() |
|
156 | self.do_one_iteration() | |
157 |
|
157 | |||
158 | def record_ports(self, xrep_port, pub_port, req_port, hb_port): |
|
158 | def record_ports(self, xrep_port, pub_port, req_port, hb_port): | |
159 | """Record the ports that this kernel is using. |
|
159 | """Record the ports that this kernel is using. | |
160 |
|
160 | |||
161 | The creator of the Kernel instance must call this methods if they |
|
161 | The creator of the Kernel instance must call this methods if they | |
162 | want the :meth:`connect_request` method to return the port numbers. |
|
162 | want the :meth:`connect_request` method to return the port numbers. | |
163 | """ |
|
163 | """ | |
164 | self._recorded_ports = { |
|
164 | self._recorded_ports = { | |
165 | 'xrep_port' : xrep_port, |
|
165 | 'xrep_port' : xrep_port, | |
166 | 'pub_port' : pub_port, |
|
166 | 'pub_port' : pub_port, | |
167 | 'req_port' : req_port, |
|
167 | 'req_port' : req_port, | |
168 | 'hb_port' : hb_port |
|
168 | 'hb_port' : hb_port | |
169 | } |
|
169 | } | |
170 |
|
170 | |||
171 | #--------------------------------------------------------------------------- |
|
171 | #--------------------------------------------------------------------------- | |
172 | # Kernel request handlers |
|
172 | # Kernel request handlers | |
173 | #--------------------------------------------------------------------------- |
|
173 | #--------------------------------------------------------------------------- | |
174 |
|
174 | |||
175 | def _publish_pyin(self, code, parent): |
|
175 | def _publish_pyin(self, code, parent): | |
176 | """Publish the code request on the pyin stream.""" |
|
176 | """Publish the code request on the pyin stream.""" | |
177 |
|
177 | |||
178 | pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent) |
|
178 | pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent) | |
179 |
|
179 | |||
180 | def execute_request(self, ident, parent): |
|
180 | def execute_request(self, ident, parent): | |
181 |
|
181 | |||
182 | status_msg = self.session.send(self.pub_socket, |
|
182 | status_msg = self.session.send(self.pub_socket, | |
183 | u'status', |
|
183 | u'status', | |
184 | {u'execution_state':u'busy'}, |
|
184 | {u'execution_state':u'busy'}, | |
185 | parent=parent |
|
185 | parent=parent | |
186 | ) |
|
186 | ) | |
187 |
|
187 | |||
188 | try: |
|
188 | try: | |
189 | content = parent[u'content'] |
|
189 | content = parent[u'content'] | |
190 | code = content[u'code'] |
|
190 | code = content[u'code'] | |
191 | silent = content[u'silent'] |
|
191 | silent = content[u'silent'] | |
192 | except: |
|
192 | except: | |
193 | logger.error("Got bad msg: ") |
|
193 | logger.error("Got bad msg: ") | |
194 | logger.error(str(Message(parent))) |
|
194 | logger.error(str(Message(parent))) | |
195 | return |
|
195 | return | |
196 |
|
196 | |||
197 | shell = self.shell # we'll need this a lot here |
|
197 | shell = self.shell # we'll need this a lot here | |
198 |
|
198 | |||
199 | # Replace raw_input. Note that is not sufficient to replace |
|
199 | # Replace raw_input. Note that is not sufficient to replace | |
200 | # raw_input in the user namespace. |
|
200 | # raw_input in the user namespace. | |
201 | raw_input = lambda prompt='': self._raw_input(prompt, ident, parent) |
|
201 | raw_input = lambda prompt='': self._raw_input(prompt, ident, parent) | |
202 | __builtin__.raw_input = raw_input |
|
202 | __builtin__.raw_input = raw_input | |
203 |
|
203 | |||
204 | # Set the parent message of the display hook and out streams. |
|
204 | # Set the parent message of the display hook and out streams. | |
205 | shell.displayhook.set_parent(parent) |
|
205 | shell.displayhook.set_parent(parent) | |
206 | shell.display_pub.set_parent(parent) |
|
206 | shell.display_pub.set_parent(parent) | |
207 | sys.stdout.set_parent(parent) |
|
207 | sys.stdout.set_parent(parent) | |
208 | sys.stderr.set_parent(parent) |
|
208 | sys.stderr.set_parent(parent) | |
209 |
|
209 | |||
210 | # Re-broadcast our input for the benefit of listening clients, and |
|
210 | # Re-broadcast our input for the benefit of listening clients, and | |
211 | # start computing output |
|
211 | # start computing output | |
212 | if not silent: |
|
212 | if not silent: | |
213 | self._publish_pyin(code, parent) |
|
213 | self._publish_pyin(code, parent) | |
214 |
|
214 | |||
215 | reply_content = {} |
|
215 | reply_content = {} | |
216 | try: |
|
216 | try: | |
217 | if silent: |
|
217 | if silent: | |
218 | # run_code uses 'exec' mode, so no displayhook will fire, and it |
|
218 | # run_code uses 'exec' mode, so no displayhook will fire, and it | |
219 | # doesn't call logging or history manipulations. Print |
|
219 | # doesn't call logging or history manipulations. Print | |
220 | # statements in that code will obviously still execute. |
|
220 | # statements in that code will obviously still execute. | |
221 | shell.run_code(code) |
|
221 | shell.run_code(code) | |
222 | else: |
|
222 | else: | |
223 | # FIXME: the shell calls the exception handler itself. |
|
223 | # FIXME: the shell calls the exception handler itself. | |
224 | shell._reply_content = None |
|
224 | shell._reply_content = None | |
225 | shell.run_cell(code) |
|
225 | shell.run_cell(code) | |
226 | except: |
|
226 | except: | |
227 | status = u'error' |
|
227 | status = u'error' | |
228 | # FIXME: this code right now isn't being used yet by default, |
|
228 | # FIXME: this code right now isn't being used yet by default, | |
229 | # because the runlines() call above directly fires off exception |
|
229 | # because the runlines() call above directly fires off exception | |
230 | # reporting. This code, therefore, is only active in the scenario |
|
230 | # reporting. This code, therefore, is only active in the scenario | |
231 | # where runlines itself has an unhandled exception. We need to |
|
231 | # where runlines itself has an unhandled exception. We need to | |
232 | # uniformize this, for all exception construction to come from a |
|
232 | # uniformize this, for all exception construction to come from a | |
233 | # single location in the codbase. |
|
233 | # single location in the codbase. | |
234 | etype, evalue, tb = sys.exc_info() |
|
234 | etype, evalue, tb = sys.exc_info() | |
235 | tb_list = traceback.format_exception(etype, evalue, tb) |
|
235 | tb_list = traceback.format_exception(etype, evalue, tb) | |
236 | reply_content.update(shell._showtraceback(etype, evalue, tb_list)) |
|
236 | reply_content.update(shell._showtraceback(etype, evalue, tb_list)) | |
237 | else: |
|
237 | else: | |
238 | status = u'ok' |
|
238 | status = u'ok' | |
239 |
|
239 | |||
240 | reply_content[u'status'] = status |
|
240 | reply_content[u'status'] = status | |
241 |
|
241 | |||
242 | # Return the execution counter so clients can display prompts |
|
242 | # Return the execution counter so clients can display prompts | |
243 | reply_content['execution_count'] = shell.execution_count -1 |
|
243 | reply_content['execution_count'] = shell.execution_count -1 | |
244 |
|
244 | |||
245 | # FIXME - fish exception info out of shell, possibly left there by |
|
245 | # FIXME - fish exception info out of shell, possibly left there by | |
246 | # runlines. We'll need to clean up this logic later. |
|
246 | # runlines. We'll need to clean up this logic later. | |
247 | if shell._reply_content is not None: |
|
247 | if shell._reply_content is not None: | |
248 | reply_content.update(shell._reply_content) |
|
248 | reply_content.update(shell._reply_content) | |
249 |
|
249 | |||
250 | # At this point, we can tell whether the main code execution succeeded |
|
250 | # At this point, we can tell whether the main code execution succeeded | |
251 | # or not. If it did, we proceed to evaluate user_variables/expressions |
|
251 | # or not. If it did, we proceed to evaluate user_variables/expressions | |
252 | if reply_content['status'] == 'ok': |
|
252 | if reply_content['status'] == 'ok': | |
253 | reply_content[u'user_variables'] = \ |
|
253 | reply_content[u'user_variables'] = \ | |
254 | shell.user_variables(content[u'user_variables']) |
|
254 | shell.user_variables(content[u'user_variables']) | |
255 | reply_content[u'user_expressions'] = \ |
|
255 | reply_content[u'user_expressions'] = \ | |
256 | shell.user_expressions(content[u'user_expressions']) |
|
256 | shell.user_expressions(content[u'user_expressions']) | |
257 | else: |
|
257 | else: | |
258 | # If there was an error, don't even try to compute variables or |
|
258 | # If there was an error, don't even try to compute variables or | |
259 | # expressions |
|
259 | # expressions | |
260 | reply_content[u'user_variables'] = {} |
|
260 | reply_content[u'user_variables'] = {} | |
261 | reply_content[u'user_expressions'] = {} |
|
261 | reply_content[u'user_expressions'] = {} | |
262 |
|
262 | |||
263 | # Payloads should be retrieved regardless of outcome, so we can both |
|
263 | # Payloads should be retrieved regardless of outcome, so we can both | |
264 | # recover partial output (that could have been generated early in a |
|
264 | # recover partial output (that could have been generated early in a | |
265 | # block, before an error) and clear the payload system always. |
|
265 | # block, before an error) and clear the payload system always. | |
266 | reply_content[u'payload'] = shell.payload_manager.read_payload() |
|
266 | reply_content[u'payload'] = shell.payload_manager.read_payload() | |
267 | # Be agressive about clearing the payload because we don't want |
|
267 | # Be agressive about clearing the payload because we don't want | |
268 | # it to sit in memory until the next execute_request comes in. |
|
268 | # it to sit in memory until the next execute_request comes in. | |
269 | shell.payload_manager.clear_payload() |
|
269 | shell.payload_manager.clear_payload() | |
270 |
|
270 | |||
271 | # Flush output before sending the reply. |
|
271 | # Flush output before sending the reply. | |
272 | sys.stdout.flush() |
|
272 | sys.stdout.flush() | |
273 | sys.stderr.flush() |
|
273 | sys.stderr.flush() | |
274 | # FIXME: on rare occasions, the flush doesn't seem to make it to the |
|
274 | # FIXME: on rare occasions, the flush doesn't seem to make it to the | |
275 | # clients... This seems to mitigate the problem, but we definitely need |
|
275 | # clients... This seems to mitigate the problem, but we definitely need | |
276 | # to better understand what's going on. |
|
276 | # to better understand what's going on. | |
277 | if self._execute_sleep: |
|
277 | if self._execute_sleep: | |
278 | time.sleep(self._execute_sleep) |
|
278 | time.sleep(self._execute_sleep) | |
279 |
|
279 | |||
280 | # Send the reply. |
|
280 | # Send the reply. | |
281 | reply_msg = self.session.send(self.reply_socket, u'execute_reply', |
|
281 | reply_msg = self.session.send(self.reply_socket, u'execute_reply', | |
282 | reply_content, parent, ident=ident) |
|
282 | reply_content, parent, ident=ident) | |
283 | logger.debug(str(reply_msg)) |
|
283 | logger.debug(str(reply_msg)) | |
284 |
|
284 | |||
285 | if reply_msg['content']['status'] == u'error': |
|
285 | if reply_msg['content']['status'] == u'error': | |
286 | self._abort_queue() |
|
286 | self._abort_queue() | |
287 |
|
287 | |||
288 | status_msg = self.session.send(self.pub_socket, |
|
288 | status_msg = self.session.send(self.pub_socket, | |
289 | u'status', |
|
289 | u'status', | |
290 | {u'execution_state':u'idle'}, |
|
290 | {u'execution_state':u'idle'}, | |
291 | parent=parent |
|
291 | parent=parent | |
292 | ) |
|
292 | ) | |
293 |
|
293 | |||
294 | def complete_request(self, ident, parent): |
|
294 | def complete_request(self, ident, parent): | |
295 | txt, matches = self._complete(parent) |
|
295 | txt, matches = self._complete(parent) | |
296 | matches = {'matches' : matches, |
|
296 | matches = {'matches' : matches, | |
297 | 'matched_text' : txt, |
|
297 | 'matched_text' : txt, | |
298 | 'status' : 'ok'} |
|
298 | 'status' : 'ok'} | |
299 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', |
|
299 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', | |
300 | matches, parent, ident) |
|
300 | matches, parent, ident) | |
301 | logger.debug(str(completion_msg)) |
|
301 | logger.debug(str(completion_msg)) | |
302 |
|
302 | |||
303 | def object_info_request(self, ident, parent): |
|
303 | def object_info_request(self, ident, parent): | |
304 | object_info = self.shell.object_inspect(parent['content']['oname']) |
|
304 | object_info = self.shell.object_inspect(parent['content']['oname']) | |
305 | # Before we send this object over, we scrub it for JSON usage |
|
305 | # Before we send this object over, we scrub it for JSON usage | |
306 | oinfo = json_clean(object_info) |
|
306 | oinfo = json_clean(object_info) | |
307 | msg = self.session.send(self.reply_socket, 'object_info_reply', |
|
307 | msg = self.session.send(self.reply_socket, 'object_info_reply', | |
308 | oinfo, parent, ident) |
|
308 | oinfo, parent, ident) | |
309 | logger.debug(msg) |
|
309 | logger.debug(msg) | |
310 |
|
310 | |||
311 | def history_request(self, ident, parent): |
|
311 | def history_tail_request(self, ident, parent): | |
312 |
# parent['content'] should contain keys " |
|
312 | # parent['content'] should contain keys "n", "raw" and "output" | |
313 | # "this_session". |
|
313 | hist = self.shell.history_manager.get_hist_tail(**parent['content']) | |
314 | hist = self.shell.get_history(**parent['content']) |
|
314 | content = {'history' : list(hist)} | |
315 | content = {'history' : hist} |
|
315 | msg = self.session.send(self.reply_socket, 'history_tail_reply', | |
316 | msg = self.session.send(self.reply_socket, 'history_reply', |
|
|||
317 | content, parent, ident) |
|
316 | content, parent, ident) | |
318 | logger.debug(str(msg)) |
|
317 | logger.debug(str(msg)) | |
319 |
|
318 | |||
320 | def connect_request(self, ident, parent): |
|
319 | def connect_request(self, ident, parent): | |
321 | if self._recorded_ports is not None: |
|
320 | if self._recorded_ports is not None: | |
322 | content = self._recorded_ports.copy() |
|
321 | content = self._recorded_ports.copy() | |
323 | else: |
|
322 | else: | |
324 | content = {} |
|
323 | content = {} | |
325 | msg = self.session.send(self.reply_socket, 'connect_reply', |
|
324 | msg = self.session.send(self.reply_socket, 'connect_reply', | |
326 | content, parent, ident) |
|
325 | content, parent, ident) | |
327 | logger.debug(msg) |
|
326 | logger.debug(msg) | |
328 |
|
327 | |||
329 | def shutdown_request(self, ident, parent): |
|
328 | def shutdown_request(self, ident, parent): | |
330 | self.shell.exit_now = True |
|
329 | self.shell.exit_now = True | |
331 | self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent) |
|
330 | self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent) | |
332 | sys.exit(0) |
|
331 | sys.exit(0) | |
333 |
|
332 | |||
334 | #--------------------------------------------------------------------------- |
|
333 | #--------------------------------------------------------------------------- | |
335 | # Protected interface |
|
334 | # Protected interface | |
336 | #--------------------------------------------------------------------------- |
|
335 | #--------------------------------------------------------------------------- | |
337 |
|
336 | |||
338 | def _abort_queue(self): |
|
337 | def _abort_queue(self): | |
339 | while True: |
|
338 | while True: | |
340 | ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK) |
|
339 | ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK) | |
341 | if msg is None: |
|
340 | if msg is None: | |
342 | break |
|
341 | break | |
343 | else: |
|
342 | else: | |
344 | assert ident is not None, \ |
|
343 | assert ident is not None, \ | |
345 | "Unexpected missing message part." |
|
344 | "Unexpected missing message part." | |
346 |
|
345 | |||
347 | logger.debug("Aborting:\n"+str(Message(msg))) |
|
346 | logger.debug("Aborting:\n"+str(Message(msg))) | |
348 | msg_type = msg['msg_type'] |
|
347 | msg_type = msg['msg_type'] | |
349 | reply_type = msg_type.split('_')[0] + '_reply' |
|
348 | reply_type = msg_type.split('_')[0] + '_reply' | |
350 | reply_msg = self.session.send(self.reply_socket, reply_type, |
|
349 | reply_msg = self.session.send(self.reply_socket, reply_type, | |
351 | {'status' : 'aborted'}, msg, ident=ident) |
|
350 | {'status' : 'aborted'}, msg, ident=ident) | |
352 | logger.debug(reply_msg) |
|
351 | logger.debug(reply_msg) | |
353 | # We need to wait a bit for requests to come in. This can probably |
|
352 | # We need to wait a bit for requests to come in. This can probably | |
354 | # be set shorter for true asynchronous clients. |
|
353 | # be set shorter for true asynchronous clients. | |
355 | time.sleep(0.1) |
|
354 | time.sleep(0.1) | |
356 |
|
355 | |||
357 | def _raw_input(self, prompt, ident, parent): |
|
356 | def _raw_input(self, prompt, ident, parent): | |
358 | # Flush output before making the request. |
|
357 | # Flush output before making the request. | |
359 | sys.stderr.flush() |
|
358 | sys.stderr.flush() | |
360 | sys.stdout.flush() |
|
359 | sys.stdout.flush() | |
361 |
|
360 | |||
362 | # Send the input request. |
|
361 | # Send the input request. | |
363 | content = dict(prompt=prompt) |
|
362 | content = dict(prompt=prompt) | |
364 | msg = self.session.send(self.req_socket, u'input_request', content, parent) |
|
363 | msg = self.session.send(self.req_socket, u'input_request', content, parent) | |
365 |
|
364 | |||
366 | # Await a response. |
|
365 | # Await a response. | |
367 | ident, reply = self.session.recv(self.req_socket, 0) |
|
366 | ident, reply = self.session.recv(self.req_socket, 0) | |
368 | try: |
|
367 | try: | |
369 | value = reply['content']['value'] |
|
368 | value = reply['content']['value'] | |
370 | except: |
|
369 | except: | |
371 | logger.error("Got bad raw_input reply: ") |
|
370 | logger.error("Got bad raw_input reply: ") | |
372 | logger.error(str(Message(parent))) |
|
371 | logger.error(str(Message(parent))) | |
373 | value = '' |
|
372 | value = '' | |
374 | return value |
|
373 | return value | |
375 |
|
374 | |||
376 | def _complete(self, msg): |
|
375 | def _complete(self, msg): | |
377 | c = msg['content'] |
|
376 | c = msg['content'] | |
378 | try: |
|
377 | try: | |
379 | cpos = int(c['cursor_pos']) |
|
378 | cpos = int(c['cursor_pos']) | |
380 | except: |
|
379 | except: | |
381 | # If we don't get something that we can convert to an integer, at |
|
380 | # If we don't get something that we can convert to an integer, at | |
382 | # least attempt the completion guessing the cursor is at the end of |
|
381 | # least attempt the completion guessing the cursor is at the end of | |
383 | # the text, if there's any, and otherwise of the line |
|
382 | # the text, if there's any, and otherwise of the line | |
384 | cpos = len(c['text']) |
|
383 | cpos = len(c['text']) | |
385 | if cpos==0: |
|
384 | if cpos==0: | |
386 | cpos = len(c['line']) |
|
385 | cpos = len(c['line']) | |
387 | return self.shell.complete(c['text'], c['line'], cpos) |
|
386 | return self.shell.complete(c['text'], c['line'], cpos) | |
388 |
|
387 | |||
389 | def _object_info(self, context): |
|
388 | def _object_info(self, context): | |
390 | symbol, leftover = self._symbol_from_context(context) |
|
389 | symbol, leftover = self._symbol_from_context(context) | |
391 | if symbol is not None and not leftover: |
|
390 | if symbol is not None and not leftover: | |
392 | doc = getattr(symbol, '__doc__', '') |
|
391 | doc = getattr(symbol, '__doc__', '') | |
393 | else: |
|
392 | else: | |
394 | doc = '' |
|
393 | doc = '' | |
395 | object_info = dict(docstring = doc) |
|
394 | object_info = dict(docstring = doc) | |
396 | return object_info |
|
395 | return object_info | |
397 |
|
396 | |||
398 | def _symbol_from_context(self, context): |
|
397 | def _symbol_from_context(self, context): | |
399 | if not context: |
|
398 | if not context: | |
400 | return None, context |
|
399 | return None, context | |
401 |
|
400 | |||
402 | base_symbol_string = context[0] |
|
401 | base_symbol_string = context[0] | |
403 | symbol = self.shell.user_ns.get(base_symbol_string, None) |
|
402 | symbol = self.shell.user_ns.get(base_symbol_string, None) | |
404 | if symbol is None: |
|
403 | if symbol is None: | |
405 | symbol = __builtin__.__dict__.get(base_symbol_string, None) |
|
404 | symbol = __builtin__.__dict__.get(base_symbol_string, None) | |
406 | if symbol is None: |
|
405 | if symbol is None: | |
407 | return None, context |
|
406 | return None, context | |
408 |
|
407 | |||
409 | context = context[1:] |
|
408 | context = context[1:] | |
410 | for i, name in enumerate(context): |
|
409 | for i, name in enumerate(context): | |
411 | new_symbol = getattr(symbol, name, None) |
|
410 | new_symbol = getattr(symbol, name, None) | |
412 | if new_symbol is None: |
|
411 | if new_symbol is None: | |
413 | return symbol, context[i:] |
|
412 | return symbol, context[i:] | |
414 | else: |
|
413 | else: | |
415 | symbol = new_symbol |
|
414 | symbol = new_symbol | |
416 |
|
415 | |||
417 | return symbol, [] |
|
416 | return symbol, [] | |
418 |
|
417 | |||
419 | def _at_shutdown(self): |
|
418 | def _at_shutdown(self): | |
420 | """Actions taken at shutdown by the kernel, called by python's atexit. |
|
419 | """Actions taken at shutdown by the kernel, called by python's atexit. | |
421 | """ |
|
420 | """ | |
422 | # io.rprint("Kernel at_shutdown") # dbg |
|
421 | # io.rprint("Kernel at_shutdown") # dbg | |
423 | if self._shutdown_message is not None: |
|
422 | if self._shutdown_message is not None: | |
424 | self.session.send(self.reply_socket, self._shutdown_message) |
|
423 | self.session.send(self.reply_socket, self._shutdown_message) | |
425 | self.session.send(self.pub_socket, self._shutdown_message) |
|
424 | self.session.send(self.pub_socket, self._shutdown_message) | |
426 | logger.debug(str(self._shutdown_message)) |
|
425 | logger.debug(str(self._shutdown_message)) | |
427 | # A very short sleep to give zmq time to flush its message buffers |
|
426 | # A very short sleep to give zmq time to flush its message buffers | |
428 | # before Python truly shuts down. |
|
427 | # before Python truly shuts down. | |
429 | time.sleep(0.01) |
|
428 | time.sleep(0.01) | |
430 |
|
429 | |||
431 |
|
430 | |||
432 | class QtKernel(Kernel): |
|
431 | class QtKernel(Kernel): | |
433 | """A Kernel subclass with Qt support.""" |
|
432 | """A Kernel subclass with Qt support.""" | |
434 |
|
433 | |||
435 | def start(self): |
|
434 | def start(self): | |
436 | """Start a kernel with QtPy4 event loop integration.""" |
|
435 | """Start a kernel with QtPy4 event loop integration.""" | |
437 |
|
436 | |||
438 | from PyQt4 import QtCore |
|
437 | from PyQt4 import QtCore | |
439 | from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4 |
|
438 | from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4 | |
440 |
|
439 | |||
441 | self.app = get_app_qt4([" "]) |
|
440 | self.app = get_app_qt4([" "]) | |
442 | self.app.setQuitOnLastWindowClosed(False) |
|
441 | self.app.setQuitOnLastWindowClosed(False) | |
443 | self.timer = QtCore.QTimer() |
|
442 | self.timer = QtCore.QTimer() | |
444 | self.timer.timeout.connect(self.do_one_iteration) |
|
443 | self.timer.timeout.connect(self.do_one_iteration) | |
445 | # Units for the timer are in milliseconds |
|
444 | # Units for the timer are in milliseconds | |
446 | self.timer.start(1000*self._poll_interval) |
|
445 | self.timer.start(1000*self._poll_interval) | |
447 | start_event_loop_qt4(self.app) |
|
446 | start_event_loop_qt4(self.app) | |
448 |
|
447 | |||
449 |
|
448 | |||
450 | class WxKernel(Kernel): |
|
449 | class WxKernel(Kernel): | |
451 | """A Kernel subclass with Wx support.""" |
|
450 | """A Kernel subclass with Wx support.""" | |
452 |
|
451 | |||
453 | def start(self): |
|
452 | def start(self): | |
454 | """Start a kernel with wx event loop support.""" |
|
453 | """Start a kernel with wx event loop support.""" | |
455 |
|
454 | |||
456 | import wx |
|
455 | import wx | |
457 | from IPython.lib.guisupport import start_event_loop_wx |
|
456 | from IPython.lib.guisupport import start_event_loop_wx | |
458 |
|
457 | |||
459 | doi = self.do_one_iteration |
|
458 | doi = self.do_one_iteration | |
460 | # Wx uses milliseconds |
|
459 | # Wx uses milliseconds | |
461 | poll_interval = int(1000*self._poll_interval) |
|
460 | poll_interval = int(1000*self._poll_interval) | |
462 |
|
461 | |||
463 | # We have to put the wx.Timer in a wx.Frame for it to fire properly. |
|
462 | # We have to put the wx.Timer in a wx.Frame for it to fire properly. | |
464 | # We make the Frame hidden when we create it in the main app below. |
|
463 | # We make the Frame hidden when we create it in the main app below. | |
465 | class TimerFrame(wx.Frame): |
|
464 | class TimerFrame(wx.Frame): | |
466 | def __init__(self, func): |
|
465 | def __init__(self, func): | |
467 | wx.Frame.__init__(self, None, -1) |
|
466 | wx.Frame.__init__(self, None, -1) | |
468 | self.timer = wx.Timer(self) |
|
467 | self.timer = wx.Timer(self) | |
469 | # Units for the timer are in milliseconds |
|
468 | # Units for the timer are in milliseconds | |
470 | self.timer.Start(poll_interval) |
|
469 | self.timer.Start(poll_interval) | |
471 | self.Bind(wx.EVT_TIMER, self.on_timer) |
|
470 | self.Bind(wx.EVT_TIMER, self.on_timer) | |
472 | self.func = func |
|
471 | self.func = func | |
473 |
|
472 | |||
474 | def on_timer(self, event): |
|
473 | def on_timer(self, event): | |
475 | self.func() |
|
474 | self.func() | |
476 |
|
475 | |||
477 | # We need a custom wx.App to create our Frame subclass that has the |
|
476 | # We need a custom wx.App to create our Frame subclass that has the | |
478 | # wx.Timer to drive the ZMQ event loop. |
|
477 | # wx.Timer to drive the ZMQ event loop. | |
479 | class IPWxApp(wx.App): |
|
478 | class IPWxApp(wx.App): | |
480 | def OnInit(self): |
|
479 | def OnInit(self): | |
481 | self.frame = TimerFrame(doi) |
|
480 | self.frame = TimerFrame(doi) | |
482 | self.frame.Show(False) |
|
481 | self.frame.Show(False) | |
483 | return True |
|
482 | return True | |
484 |
|
483 | |||
485 | # The redirect=False here makes sure that wx doesn't replace |
|
484 | # The redirect=False here makes sure that wx doesn't replace | |
486 | # sys.stdout/stderr with its own classes. |
|
485 | # sys.stdout/stderr with its own classes. | |
487 | self.app = IPWxApp(redirect=False) |
|
486 | self.app = IPWxApp(redirect=False) | |
488 | start_event_loop_wx(self.app) |
|
487 | start_event_loop_wx(self.app) | |
489 |
|
488 | |||
490 |
|
489 | |||
491 | class TkKernel(Kernel): |
|
490 | class TkKernel(Kernel): | |
492 | """A Kernel subclass with Tk support.""" |
|
491 | """A Kernel subclass with Tk support.""" | |
493 |
|
492 | |||
494 | def start(self): |
|
493 | def start(self): | |
495 | """Start a Tk enabled event loop.""" |
|
494 | """Start a Tk enabled event loop.""" | |
496 |
|
495 | |||
497 | import Tkinter |
|
496 | import Tkinter | |
498 | doi = self.do_one_iteration |
|
497 | doi = self.do_one_iteration | |
499 | # Tk uses milliseconds |
|
498 | # Tk uses milliseconds | |
500 | poll_interval = int(1000*self._poll_interval) |
|
499 | poll_interval = int(1000*self._poll_interval) | |
501 | # For Tkinter, we create a Tk object and call its withdraw method. |
|
500 | # For Tkinter, we create a Tk object and call its withdraw method. | |
502 | class Timer(object): |
|
501 | class Timer(object): | |
503 | def __init__(self, func): |
|
502 | def __init__(self, func): | |
504 | self.app = Tkinter.Tk() |
|
503 | self.app = Tkinter.Tk() | |
505 | self.app.withdraw() |
|
504 | self.app.withdraw() | |
506 | self.func = func |
|
505 | self.func = func | |
507 |
|
506 | |||
508 | def on_timer(self): |
|
507 | def on_timer(self): | |
509 | self.func() |
|
508 | self.func() | |
510 | self.app.after(poll_interval, self.on_timer) |
|
509 | self.app.after(poll_interval, self.on_timer) | |
511 |
|
510 | |||
512 | def start(self): |
|
511 | def start(self): | |
513 | self.on_timer() # Call it once to get things going. |
|
512 | self.on_timer() # Call it once to get things going. | |
514 | self.app.mainloop() |
|
513 | self.app.mainloop() | |
515 |
|
514 | |||
516 | self.timer = Timer(doi) |
|
515 | self.timer = Timer(doi) | |
517 | self.timer.start() |
|
516 | self.timer.start() | |
518 |
|
517 | |||
519 |
|
518 | |||
520 | class GTKKernel(Kernel): |
|
519 | class GTKKernel(Kernel): | |
521 | """A Kernel subclass with GTK support.""" |
|
520 | """A Kernel subclass with GTK support.""" | |
522 |
|
521 | |||
523 | def start(self): |
|
522 | def start(self): | |
524 | """Start the kernel, coordinating with the GTK event loop""" |
|
523 | """Start the kernel, coordinating with the GTK event loop""" | |
525 | from .gui.gtkembed import GTKEmbed |
|
524 | from .gui.gtkembed import GTKEmbed | |
526 |
|
525 | |||
527 | gtk_kernel = GTKEmbed(self) |
|
526 | gtk_kernel = GTKEmbed(self) | |
528 | gtk_kernel.start() |
|
527 | gtk_kernel.start() | |
529 |
|
528 | |||
530 |
|
529 | |||
531 | #----------------------------------------------------------------------------- |
|
530 | #----------------------------------------------------------------------------- | |
532 | # Kernel main and launch functions |
|
531 | # Kernel main and launch functions | |
533 | #----------------------------------------------------------------------------- |
|
532 | #----------------------------------------------------------------------------- | |
534 |
|
533 | |||
535 | def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, |
|
534 | def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, | |
536 | independent=False, pylab=False, colors=None): |
|
535 | independent=False, pylab=False, colors=None): | |
537 | """Launches a localhost kernel, binding to the specified ports. |
|
536 | """Launches a localhost kernel, binding to the specified ports. | |
538 |
|
537 | |||
539 | Parameters |
|
538 | Parameters | |
540 | ---------- |
|
539 | ---------- | |
541 | ip : str, optional |
|
540 | ip : str, optional | |
542 | The ip address the kernel will bind to. |
|
541 | The ip address the kernel will bind to. | |
543 |
|
542 | |||
544 | xrep_port : int, optional |
|
543 | xrep_port : int, optional | |
545 | The port to use for XREP channel. |
|
544 | The port to use for XREP channel. | |
546 |
|
545 | |||
547 | pub_port : int, optional |
|
546 | pub_port : int, optional | |
548 | The port to use for the SUB channel. |
|
547 | The port to use for the SUB channel. | |
549 |
|
548 | |||
550 | req_port : int, optional |
|
549 | req_port : int, optional | |
551 | The port to use for the REQ (raw input) channel. |
|
550 | The port to use for the REQ (raw input) channel. | |
552 |
|
551 | |||
553 | hb_port : int, optional |
|
552 | hb_port : int, optional | |
554 | The port to use for the hearbeat REP channel. |
|
553 | The port to use for the hearbeat REP channel. | |
555 |
|
554 | |||
556 | independent : bool, optional (default False) |
|
555 | independent : bool, optional (default False) | |
557 | If set, the kernel process is guaranteed to survive if this process |
|
556 | If set, the kernel process is guaranteed to survive if this process | |
558 | dies. If not set, an effort is made to ensure that the kernel is killed |
|
557 | dies. If not set, an effort is made to ensure that the kernel is killed | |
559 | when this process dies. Note that in this case it is still good practice |
|
558 | when this process dies. Note that in this case it is still good practice | |
560 | to kill kernels manually before exiting. |
|
559 | to kill kernels manually before exiting. | |
561 |
|
560 | |||
562 | pylab : bool or string, optional (default False) |
|
561 | pylab : bool or string, optional (default False) | |
563 | If not False, the kernel will be launched with pylab enabled. If a |
|
562 | If not False, the kernel will be launched with pylab enabled. If a | |
564 | string is passed, matplotlib will use the specified backend. Otherwise, |
|
563 | string is passed, matplotlib will use the specified backend. Otherwise, | |
565 | matplotlib's default backend will be used. |
|
564 | matplotlib's default backend will be used. | |
566 |
|
565 | |||
567 | colors : None or string, optional (default None) |
|
566 | colors : None or string, optional (default None) | |
568 | If not None, specify the color scheme. One of (NoColor, LightBG, Linux) |
|
567 | If not None, specify the color scheme. One of (NoColor, LightBG, Linux) | |
569 |
|
568 | |||
570 | Returns |
|
569 | Returns | |
571 | ------- |
|
570 | ------- | |
572 | A tuple of form: |
|
571 | A tuple of form: | |
573 | (kernel_process, xrep_port, pub_port, req_port) |
|
572 | (kernel_process, xrep_port, pub_port, req_port) | |
574 | where kernel_process is a Popen object and the ports are integers. |
|
573 | where kernel_process is a Popen object and the ports are integers. | |
575 | """ |
|
574 | """ | |
576 | extra_arguments = [] |
|
575 | extra_arguments = [] | |
577 | if pylab: |
|
576 | if pylab: | |
578 | extra_arguments.append('--pylab') |
|
577 | extra_arguments.append('--pylab') | |
579 | if isinstance(pylab, basestring): |
|
578 | if isinstance(pylab, basestring): | |
580 | extra_arguments.append(pylab) |
|
579 | extra_arguments.append(pylab) | |
581 | if ip is not None: |
|
580 | if ip is not None: | |
582 | extra_arguments.append('--ip') |
|
581 | extra_arguments.append('--ip') | |
583 | if isinstance(ip, basestring): |
|
582 | if isinstance(ip, basestring): | |
584 | extra_arguments.append(ip) |
|
583 | extra_arguments.append(ip) | |
585 | if colors is not None: |
|
584 | if colors is not None: | |
586 | extra_arguments.append('--colors') |
|
585 | extra_arguments.append('--colors') | |
587 | extra_arguments.append(colors) |
|
586 | extra_arguments.append(colors) | |
588 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', |
|
587 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', | |
589 | xrep_port, pub_port, req_port, hb_port, |
|
588 | xrep_port, pub_port, req_port, hb_port, | |
590 | independent, extra_arguments) |
|
589 | independent, extra_arguments) | |
591 |
|
590 | |||
592 |
|
591 | |||
593 | def main(): |
|
592 | def main(): | |
594 | """ The IPython kernel main entry point. |
|
593 | """ The IPython kernel main entry point. | |
595 | """ |
|
594 | """ | |
596 | parser = make_argument_parser() |
|
595 | parser = make_argument_parser() | |
597 | parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
596 | parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | |
598 | const='auto', help = \ |
|
597 | const='auto', help = \ | |
599 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
598 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | |
600 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
599 | given, the GUI backend is matplotlib's, otherwise use one of: \ | |
601 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") |
|
600 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") | |
602 | parser.add_argument('--colors', |
|
601 | parser.add_argument('--colors', | |
603 | type=str, dest='colors', |
|
602 | type=str, dest='colors', | |
604 | help="Set the color scheme (NoColor, Linux, and LightBG).", |
|
603 | help="Set the color scheme (NoColor, Linux, and LightBG).", | |
605 | metavar='ZMQInteractiveShell.colors') |
|
604 | metavar='ZMQInteractiveShell.colors') | |
606 | namespace = parser.parse_args() |
|
605 | namespace = parser.parse_args() | |
607 |
|
606 | |||
608 | kernel_class = Kernel |
|
607 | kernel_class = Kernel | |
609 |
|
608 | |||
610 | kernel_classes = { |
|
609 | kernel_classes = { | |
611 | 'qt' : QtKernel, |
|
610 | 'qt' : QtKernel, | |
612 | 'qt4': QtKernel, |
|
611 | 'qt4': QtKernel, | |
613 | 'inline': Kernel, |
|
612 | 'inline': Kernel, | |
614 | 'wx' : WxKernel, |
|
613 | 'wx' : WxKernel, | |
615 | 'tk' : TkKernel, |
|
614 | 'tk' : TkKernel, | |
616 | 'gtk': GTKKernel, |
|
615 | 'gtk': GTKKernel, | |
617 | } |
|
616 | } | |
618 | if namespace.pylab: |
|
617 | if namespace.pylab: | |
619 | if namespace.pylab == 'auto': |
|
618 | if namespace.pylab == 'auto': | |
620 | gui, backend = pylabtools.find_gui_and_backend() |
|
619 | gui, backend = pylabtools.find_gui_and_backend() | |
621 | else: |
|
620 | else: | |
622 | gui, backend = pylabtools.find_gui_and_backend(namespace.pylab) |
|
621 | gui, backend = pylabtools.find_gui_and_backend(namespace.pylab) | |
623 | kernel_class = kernel_classes.get(gui) |
|
622 | kernel_class = kernel_classes.get(gui) | |
624 | if kernel_class is None: |
|
623 | if kernel_class is None: | |
625 | raise ValueError('GUI is not supported: %r' % gui) |
|
624 | raise ValueError('GUI is not supported: %r' % gui) | |
626 | pylabtools.activate_matplotlib(backend) |
|
625 | pylabtools.activate_matplotlib(backend) | |
627 | if namespace.colors: |
|
626 | if namespace.colors: | |
628 | ZMQInteractiveShell.colors=namespace.colors |
|
627 | ZMQInteractiveShell.colors=namespace.colors | |
629 |
|
628 | |||
630 | kernel = make_kernel(namespace, kernel_class, OutStream) |
|
629 | kernel = make_kernel(namespace, kernel_class, OutStream) | |
631 |
|
630 | |||
632 | if namespace.pylab: |
|
631 | if namespace.pylab: | |
633 | pylabtools.import_pylab(kernel.shell.user_ns, backend, |
|
632 | pylabtools.import_pylab(kernel.shell.user_ns, backend, | |
634 | shell=kernel.shell) |
|
633 | shell=kernel.shell) | |
635 |
|
634 | |||
636 | start_kernel(namespace, kernel) |
|
635 | start_kernel(namespace, kernel) | |
637 |
|
636 | |||
638 |
|
637 | |||
639 | if __name__ == '__main__': |
|
638 | if __name__ == '__main__': | |
640 | main() |
|
639 | main() |
@@ -1,914 +1,908 b'' | |||||
1 | """Base classes to manage the interaction with a running kernel. |
|
1 | """Base classes to manage the interaction with a running kernel. | |
2 |
|
2 | |||
3 | TODO |
|
3 | TODO | |
4 | * Create logger to handle debugging and console messages. |
|
4 | * Create logger to handle debugging and console messages. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008-2010 The IPython Development Team |
|
8 | # Copyright (C) 2008-2010 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | # Standard library imports. |
|
18 | # Standard library imports. | |
19 | import atexit |
|
19 | import atexit | |
20 | from Queue import Queue, Empty |
|
20 | from Queue import Queue, Empty | |
21 | from subprocess import Popen |
|
21 | from subprocess import Popen | |
22 | import signal |
|
22 | import signal | |
23 | import sys |
|
23 | import sys | |
24 | from threading import Thread |
|
24 | from threading import Thread | |
25 | import time |
|
25 | import time | |
26 | import logging |
|
26 | import logging | |
27 |
|
27 | |||
28 | # System library imports. |
|
28 | # System library imports. | |
29 | import zmq |
|
29 | import zmq | |
30 | from zmq import POLLIN, POLLOUT, POLLERR |
|
30 | from zmq import POLLIN, POLLOUT, POLLERR | |
31 | from zmq.eventloop import ioloop |
|
31 | from zmq.eventloop import ioloop | |
32 |
|
32 | |||
33 | # Local imports. |
|
33 | # Local imports. | |
34 | from IPython.utils import io |
|
34 | from IPython.utils import io | |
35 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS |
|
35 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | |
36 | from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress |
|
36 | from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress | |
37 | from session import Session, Message |
|
37 | from session import Session, Message | |
38 |
|
38 | |||
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | # Constants and exceptions |
|
40 | # Constants and exceptions | |
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 |
|
42 | |||
43 | class InvalidPortNumber(Exception): |
|
43 | class InvalidPortNumber(Exception): | |
44 | pass |
|
44 | pass | |
45 |
|
45 | |||
46 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
47 | # Utility functions |
|
47 | # Utility functions | |
48 | #----------------------------------------------------------------------------- |
|
48 | #----------------------------------------------------------------------------- | |
49 |
|
49 | |||
50 | # some utilities to validate message structure, these might get moved elsewhere |
|
50 | # some utilities to validate message structure, these might get moved elsewhere | |
51 | # if they prove to have more generic utility |
|
51 | # if they prove to have more generic utility | |
52 |
|
52 | |||
53 | def validate_string_list(lst): |
|
53 | def validate_string_list(lst): | |
54 | """Validate that the input is a list of strings. |
|
54 | """Validate that the input is a list of strings. | |
55 |
|
55 | |||
56 | Raises ValueError if not.""" |
|
56 | Raises ValueError if not.""" | |
57 | if not isinstance(lst, list): |
|
57 | if not isinstance(lst, list): | |
58 | raise ValueError('input %r must be a list' % lst) |
|
58 | raise ValueError('input %r must be a list' % lst) | |
59 | for x in lst: |
|
59 | for x in lst: | |
60 | if not isinstance(x, basestring): |
|
60 | if not isinstance(x, basestring): | |
61 | raise ValueError('element %r in list must be a string' % x) |
|
61 | raise ValueError('element %r in list must be a string' % x) | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | def validate_string_dict(dct): |
|
64 | def validate_string_dict(dct): | |
65 | """Validate that the input is a dict with string keys and values. |
|
65 | """Validate that the input is a dict with string keys and values. | |
66 |
|
66 | |||
67 | Raises ValueError if not.""" |
|
67 | Raises ValueError if not.""" | |
68 | for k,v in dct.iteritems(): |
|
68 | for k,v in dct.iteritems(): | |
69 | if not isinstance(k, basestring): |
|
69 | if not isinstance(k, basestring): | |
70 | raise ValueError('key %r in dict must be a string' % k) |
|
70 | raise ValueError('key %r in dict must be a string' % k) | |
71 | if not isinstance(v, basestring): |
|
71 | if not isinstance(v, basestring): | |
72 | raise ValueError('value %r in dict must be a string' % v) |
|
72 | raise ValueError('value %r in dict must be a string' % v) | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | #----------------------------------------------------------------------------- |
|
75 | #----------------------------------------------------------------------------- | |
76 | # ZMQ Socket Channel classes |
|
76 | # ZMQ Socket Channel classes | |
77 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
78 |
|
78 | |||
79 | class ZmqSocketChannel(Thread): |
|
79 | class ZmqSocketChannel(Thread): | |
80 | """The base class for the channels that use ZMQ sockets. |
|
80 | """The base class for the channels that use ZMQ sockets. | |
81 | """ |
|
81 | """ | |
82 | context = None |
|
82 | context = None | |
83 | session = None |
|
83 | session = None | |
84 | socket = None |
|
84 | socket = None | |
85 | ioloop = None |
|
85 | ioloop = None | |
86 | iostate = None |
|
86 | iostate = None | |
87 | _address = None |
|
87 | _address = None | |
88 |
|
88 | |||
89 | def __init__(self, context, session, address): |
|
89 | def __init__(self, context, session, address): | |
90 | """Create a channel |
|
90 | """Create a channel | |
91 |
|
91 | |||
92 | Parameters |
|
92 | Parameters | |
93 | ---------- |
|
93 | ---------- | |
94 | context : :class:`zmq.Context` |
|
94 | context : :class:`zmq.Context` | |
95 | The ZMQ context to use. |
|
95 | The ZMQ context to use. | |
96 | session : :class:`session.Session` |
|
96 | session : :class:`session.Session` | |
97 | The session to use. |
|
97 | The session to use. | |
98 | address : tuple |
|
98 | address : tuple | |
99 | Standard (ip, port) tuple that the kernel is listening on. |
|
99 | Standard (ip, port) tuple that the kernel is listening on. | |
100 | """ |
|
100 | """ | |
101 | super(ZmqSocketChannel, self).__init__() |
|
101 | super(ZmqSocketChannel, self).__init__() | |
102 | self.daemon = True |
|
102 | self.daemon = True | |
103 |
|
103 | |||
104 | self.context = context |
|
104 | self.context = context | |
105 | self.session = session |
|
105 | self.session = session | |
106 | if address[1] == 0: |
|
106 | if address[1] == 0: | |
107 | message = 'The port number for a channel cannot be 0.' |
|
107 | message = 'The port number for a channel cannot be 0.' | |
108 | raise InvalidPortNumber(message) |
|
108 | raise InvalidPortNumber(message) | |
109 | self._address = address |
|
109 | self._address = address | |
110 |
|
110 | |||
111 | def stop(self): |
|
111 | def stop(self): | |
112 | """Stop the channel's activity. |
|
112 | """Stop the channel's activity. | |
113 |
|
113 | |||
114 | This calls :method:`Thread.join` and returns when the thread |
|
114 | This calls :method:`Thread.join` and returns when the thread | |
115 | terminates. :class:`RuntimeError` will be raised if |
|
115 | terminates. :class:`RuntimeError` will be raised if | |
116 | :method:`self.start` is called again. |
|
116 | :method:`self.start` is called again. | |
117 | """ |
|
117 | """ | |
118 | self.join() |
|
118 | self.join() | |
119 |
|
119 | |||
120 | @property |
|
120 | @property | |
121 | def address(self): |
|
121 | def address(self): | |
122 | """Get the channel's address as an (ip, port) tuple. |
|
122 | """Get the channel's address as an (ip, port) tuple. | |
123 |
|
123 | |||
124 | By the default, the address is (localhost, 0), where 0 means a random |
|
124 | By the default, the address is (localhost, 0), where 0 means a random | |
125 | port. |
|
125 | port. | |
126 | """ |
|
126 | """ | |
127 | return self._address |
|
127 | return self._address | |
128 |
|
128 | |||
129 | def add_io_state(self, state): |
|
129 | def add_io_state(self, state): | |
130 | """Add IO state to the eventloop. |
|
130 | """Add IO state to the eventloop. | |
131 |
|
131 | |||
132 | Parameters |
|
132 | Parameters | |
133 | ---------- |
|
133 | ---------- | |
134 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR |
|
134 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR | |
135 | The IO state flag to set. |
|
135 | The IO state flag to set. | |
136 |
|
136 | |||
137 | This is thread safe as it uses the thread safe IOLoop.add_callback. |
|
137 | This is thread safe as it uses the thread safe IOLoop.add_callback. | |
138 | """ |
|
138 | """ | |
139 | def add_io_state_callback(): |
|
139 | def add_io_state_callback(): | |
140 | if not self.iostate & state: |
|
140 | if not self.iostate & state: | |
141 | self.iostate = self.iostate | state |
|
141 | self.iostate = self.iostate | state | |
142 | self.ioloop.update_handler(self.socket, self.iostate) |
|
142 | self.ioloop.update_handler(self.socket, self.iostate) | |
143 | self.ioloop.add_callback(add_io_state_callback) |
|
143 | self.ioloop.add_callback(add_io_state_callback) | |
144 |
|
144 | |||
145 | def drop_io_state(self, state): |
|
145 | def drop_io_state(self, state): | |
146 | """Drop IO state from the eventloop. |
|
146 | """Drop IO state from the eventloop. | |
147 |
|
147 | |||
148 | Parameters |
|
148 | Parameters | |
149 | ---------- |
|
149 | ---------- | |
150 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR |
|
150 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR | |
151 | The IO state flag to set. |
|
151 | The IO state flag to set. | |
152 |
|
152 | |||
153 | This is thread safe as it uses the thread safe IOLoop.add_callback. |
|
153 | This is thread safe as it uses the thread safe IOLoop.add_callback. | |
154 | """ |
|
154 | """ | |
155 | def drop_io_state_callback(): |
|
155 | def drop_io_state_callback(): | |
156 | if self.iostate & state: |
|
156 | if self.iostate & state: | |
157 | self.iostate = self.iostate & (~state) |
|
157 | self.iostate = self.iostate & (~state) | |
158 | self.ioloop.update_handler(self.socket, self.iostate) |
|
158 | self.ioloop.update_handler(self.socket, self.iostate) | |
159 | self.ioloop.add_callback(drop_io_state_callback) |
|
159 | self.ioloop.add_callback(drop_io_state_callback) | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | class XReqSocketChannel(ZmqSocketChannel): |
|
162 | class XReqSocketChannel(ZmqSocketChannel): | |
163 | """The XREQ channel for issues request/replies to the kernel. |
|
163 | """The XREQ channel for issues request/replies to the kernel. | |
164 | """ |
|
164 | """ | |
165 |
|
165 | |||
166 | command_queue = None |
|
166 | command_queue = None | |
167 |
|
167 | |||
168 | def __init__(self, context, session, address): |
|
168 | def __init__(self, context, session, address): | |
169 | super(XReqSocketChannel, self).__init__(context, session, address) |
|
169 | super(XReqSocketChannel, self).__init__(context, session, address) | |
170 | self.command_queue = Queue() |
|
170 | self.command_queue = Queue() | |
171 | self.ioloop = ioloop.IOLoop() |
|
171 | self.ioloop = ioloop.IOLoop() | |
172 |
|
172 | |||
173 | def run(self): |
|
173 | def run(self): | |
174 | """The thread's main activity. Call start() instead.""" |
|
174 | """The thread's main activity. Call start() instead.""" | |
175 | self.socket = self.context.socket(zmq.XREQ) |
|
175 | self.socket = self.context.socket(zmq.XREQ) | |
176 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
176 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
177 | self.socket.connect('tcp://%s:%i' % self.address) |
|
177 | self.socket.connect('tcp://%s:%i' % self.address) | |
178 | self.iostate = POLLERR|POLLIN |
|
178 | self.iostate = POLLERR|POLLIN | |
179 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
179 | self.ioloop.add_handler(self.socket, self._handle_events, | |
180 | self.iostate) |
|
180 | self.iostate) | |
181 | self.ioloop.start() |
|
181 | self.ioloop.start() | |
182 |
|
182 | |||
183 | def stop(self): |
|
183 | def stop(self): | |
184 | self.ioloop.stop() |
|
184 | self.ioloop.stop() | |
185 | super(XReqSocketChannel, self).stop() |
|
185 | super(XReqSocketChannel, self).stop() | |
186 |
|
186 | |||
187 | def call_handlers(self, msg): |
|
187 | def call_handlers(self, msg): | |
188 | """This method is called in the ioloop thread when a message arrives. |
|
188 | """This method is called in the ioloop thread when a message arrives. | |
189 |
|
189 | |||
190 | Subclasses should override this method to handle incoming messages. |
|
190 | Subclasses should override this method to handle incoming messages. | |
191 | It is important to remember that this method is called in the thread |
|
191 | It is important to remember that this method is called in the thread | |
192 | so that some logic must be done to ensure that the application leve |
|
192 | so that some logic must be done to ensure that the application leve | |
193 | handlers are called in the application thread. |
|
193 | handlers are called in the application thread. | |
194 | """ |
|
194 | """ | |
195 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
195 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
196 |
|
196 | |||
197 | def execute(self, code, silent=False, |
|
197 | def execute(self, code, silent=False, | |
198 | user_variables=None, user_expressions=None): |
|
198 | user_variables=None, user_expressions=None): | |
199 | """Execute code in the kernel. |
|
199 | """Execute code in the kernel. | |
200 |
|
200 | |||
201 | Parameters |
|
201 | Parameters | |
202 | ---------- |
|
202 | ---------- | |
203 | code : str |
|
203 | code : str | |
204 | A string of Python code. |
|
204 | A string of Python code. | |
205 |
|
205 | |||
206 | silent : bool, optional (default False) |
|
206 | silent : bool, optional (default False) | |
207 | If set, the kernel will execute the code as quietly possible. |
|
207 | If set, the kernel will execute the code as quietly possible. | |
208 |
|
208 | |||
209 | user_variables : list, optional |
|
209 | user_variables : list, optional | |
210 | A list of variable names to pull from the user's namespace. They |
|
210 | A list of variable names to pull from the user's namespace. They | |
211 | will come back as a dict with these names as keys and their |
|
211 | will come back as a dict with these names as keys and their | |
212 | :func:`repr` as values. |
|
212 | :func:`repr` as values. | |
213 |
|
213 | |||
214 | user_expressions : dict, optional |
|
214 | user_expressions : dict, optional | |
215 | A dict with string keys and to pull from the user's |
|
215 | A dict with string keys and to pull from the user's | |
216 | namespace. They will come back as a dict with these names as keys |
|
216 | namespace. They will come back as a dict with these names as keys | |
217 | and their :func:`repr` as values. |
|
217 | and their :func:`repr` as values. | |
218 |
|
218 | |||
219 | Returns |
|
219 | Returns | |
220 | ------- |
|
220 | ------- | |
221 | The msg_id of the message sent. |
|
221 | The msg_id of the message sent. | |
222 | """ |
|
222 | """ | |
223 | if user_variables is None: |
|
223 | if user_variables is None: | |
224 | user_variables = [] |
|
224 | user_variables = [] | |
225 | if user_expressions is None: |
|
225 | if user_expressions is None: | |
226 | user_expressions = {} |
|
226 | user_expressions = {} | |
227 |
|
227 | |||
228 | # Don't waste network traffic if inputs are invalid |
|
228 | # Don't waste network traffic if inputs are invalid | |
229 | if not isinstance(code, basestring): |
|
229 | if not isinstance(code, basestring): | |
230 | raise ValueError('code %r must be a string' % code) |
|
230 | raise ValueError('code %r must be a string' % code) | |
231 | validate_string_list(user_variables) |
|
231 | validate_string_list(user_variables) | |
232 | validate_string_dict(user_expressions) |
|
232 | validate_string_dict(user_expressions) | |
233 |
|
233 | |||
234 | # Create class for content/msg creation. Related to, but possibly |
|
234 | # Create class for content/msg creation. Related to, but possibly | |
235 | # not in Session. |
|
235 | # not in Session. | |
236 | content = dict(code=code, silent=silent, |
|
236 | content = dict(code=code, silent=silent, | |
237 | user_variables=user_variables, |
|
237 | user_variables=user_variables, | |
238 | user_expressions=user_expressions) |
|
238 | user_expressions=user_expressions) | |
239 | msg = self.session.msg('execute_request', content) |
|
239 | msg = self.session.msg('execute_request', content) | |
240 | self._queue_request(msg) |
|
240 | self._queue_request(msg) | |
241 | return msg['header']['msg_id'] |
|
241 | return msg['header']['msg_id'] | |
242 |
|
242 | |||
243 | def complete(self, text, line, cursor_pos, block=None): |
|
243 | def complete(self, text, line, cursor_pos, block=None): | |
244 | """Tab complete text in the kernel's namespace. |
|
244 | """Tab complete text in the kernel's namespace. | |
245 |
|
245 | |||
246 | Parameters |
|
246 | Parameters | |
247 | ---------- |
|
247 | ---------- | |
248 | text : str |
|
248 | text : str | |
249 | The text to complete. |
|
249 | The text to complete. | |
250 | line : str |
|
250 | line : str | |
251 | The full line of text that is the surrounding context for the |
|
251 | The full line of text that is the surrounding context for the | |
252 | text to complete. |
|
252 | text to complete. | |
253 | cursor_pos : int |
|
253 | cursor_pos : int | |
254 | The position of the cursor in the line where the completion was |
|
254 | The position of the cursor in the line where the completion was | |
255 | requested. |
|
255 | requested. | |
256 | block : str, optional |
|
256 | block : str, optional | |
257 | The full block of code in which the completion is being requested. |
|
257 | The full block of code in which the completion is being requested. | |
258 |
|
258 | |||
259 | Returns |
|
259 | Returns | |
260 | ------- |
|
260 | ------- | |
261 | The msg_id of the message sent. |
|
261 | The msg_id of the message sent. | |
262 | """ |
|
262 | """ | |
263 | content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos) |
|
263 | content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos) | |
264 | msg = self.session.msg('complete_request', content) |
|
264 | msg = self.session.msg('complete_request', content) | |
265 | self._queue_request(msg) |
|
265 | self._queue_request(msg) | |
266 | return msg['header']['msg_id'] |
|
266 | return msg['header']['msg_id'] | |
267 |
|
267 | |||
268 | def object_info(self, oname): |
|
268 | def object_info(self, oname): | |
269 | """Get metadata information about an object. |
|
269 | """Get metadata information about an object. | |
270 |
|
270 | |||
271 | Parameters |
|
271 | Parameters | |
272 | ---------- |
|
272 | ---------- | |
273 | oname : str |
|
273 | oname : str | |
274 | A string specifying the object name. |
|
274 | A string specifying the object name. | |
275 |
|
275 | |||
276 | Returns |
|
276 | Returns | |
277 | ------- |
|
277 | ------- | |
278 | The msg_id of the message sent. |
|
278 | The msg_id of the message sent. | |
279 | """ |
|
279 | """ | |
280 | content = dict(oname=oname) |
|
280 | content = dict(oname=oname) | |
281 | msg = self.session.msg('object_info_request', content) |
|
281 | msg = self.session.msg('object_info_request', content) | |
282 | self._queue_request(msg) |
|
282 | self._queue_request(msg) | |
283 | return msg['header']['msg_id'] |
|
283 | return msg['header']['msg_id'] | |
284 |
|
284 | |||
285 |
def history(self, |
|
285 | def history_tail(self, n=10, raw=True, output=False): | |
286 | """Get the history list. |
|
286 | """Get the history list. | |
287 |
|
287 | |||
288 | Parameters |
|
288 | Parameters | |
289 | ---------- |
|
289 | ---------- | |
290 | index : n or (n1, n2) or None |
|
290 | n : int | |
291 | If n, then the last entries. If a tuple, then all in |
|
291 | The number of lines of history to get. | |
292 | range(n1, n2). If None, then all entries. Raises IndexError if |
|
|||
293 | the format of index is incorrect. |
|
|||
294 | raw : bool |
|
292 | raw : bool | |
295 | If True, return the raw input. |
|
293 | If True, return the raw input. | |
296 | output : bool |
|
294 | output : bool | |
297 | If True, then return the output as well. |
|
295 | If True, then return the output as well. | |
298 | this_session : bool |
|
|||
299 | If True, returns only history from the current session. Otherwise, |
|
|||
300 | includes reloaded history from previous sessions. |
|
|||
301 |
|
296 | |||
302 | Returns |
|
297 | Returns | |
303 | ------- |
|
298 | ------- | |
304 | The msg_id of the message sent. |
|
299 | The msg_id of the message sent. | |
305 | """ |
|
300 | """ | |
306 |
content = dict( |
|
301 | content = dict(n=n, raw=raw, output=output) | |
307 | this_session=this_session) |
|
302 | msg = self.session.msg('history_tail_request', content) | |
308 | msg = self.session.msg('history_request', content) |
|
|||
309 | self._queue_request(msg) |
|
303 | self._queue_request(msg) | |
310 | return msg['header']['msg_id'] |
|
304 | return msg['header']['msg_id'] | |
311 |
|
305 | |||
312 | def shutdown(self, restart=False): |
|
306 | def shutdown(self, restart=False): | |
313 | """Request an immediate kernel shutdown. |
|
307 | """Request an immediate kernel shutdown. | |
314 |
|
308 | |||
315 | Upon receipt of the (empty) reply, client code can safely assume that |
|
309 | Upon receipt of the (empty) reply, client code can safely assume that | |
316 | the kernel has shut down and it's safe to forcefully terminate it if |
|
310 | the kernel has shut down and it's safe to forcefully terminate it if | |
317 | it's still alive. |
|
311 | it's still alive. | |
318 |
|
312 | |||
319 | The kernel will send the reply via a function registered with Python's |
|
313 | The kernel will send the reply via a function registered with Python's | |
320 | atexit module, ensuring it's truly done as the kernel is done with all |
|
314 | atexit module, ensuring it's truly done as the kernel is done with all | |
321 | normal operation. |
|
315 | normal operation. | |
322 | """ |
|
316 | """ | |
323 | # Send quit message to kernel. Once we implement kernel-side setattr, |
|
317 | # Send quit message to kernel. Once we implement kernel-side setattr, | |
324 | # this should probably be done that way, but for now this will do. |
|
318 | # this should probably be done that way, but for now this will do. | |
325 | msg = self.session.msg('shutdown_request', {'restart':restart}) |
|
319 | msg = self.session.msg('shutdown_request', {'restart':restart}) | |
326 | self._queue_request(msg) |
|
320 | self._queue_request(msg) | |
327 | return msg['header']['msg_id'] |
|
321 | return msg['header']['msg_id'] | |
328 |
|
322 | |||
329 | def _handle_events(self, socket, events): |
|
323 | def _handle_events(self, socket, events): | |
330 | if events & POLLERR: |
|
324 | if events & POLLERR: | |
331 | self._handle_err() |
|
325 | self._handle_err() | |
332 | if events & POLLOUT: |
|
326 | if events & POLLOUT: | |
333 | self._handle_send() |
|
327 | self._handle_send() | |
334 | if events & POLLIN: |
|
328 | if events & POLLIN: | |
335 | self._handle_recv() |
|
329 | self._handle_recv() | |
336 |
|
330 | |||
337 | def _handle_recv(self): |
|
331 | def _handle_recv(self): | |
338 | ident,msg = self.session.recv(self.socket, 0) |
|
332 | ident,msg = self.session.recv(self.socket, 0) | |
339 | self.call_handlers(msg) |
|
333 | self.call_handlers(msg) | |
340 |
|
334 | |||
341 | def _handle_send(self): |
|
335 | def _handle_send(self): | |
342 | try: |
|
336 | try: | |
343 | msg = self.command_queue.get(False) |
|
337 | msg = self.command_queue.get(False) | |
344 | except Empty: |
|
338 | except Empty: | |
345 | pass |
|
339 | pass | |
346 | else: |
|
340 | else: | |
347 | self.session.send(self.socket,msg) |
|
341 | self.session.send(self.socket,msg) | |
348 | if self.command_queue.empty(): |
|
342 | if self.command_queue.empty(): | |
349 | self.drop_io_state(POLLOUT) |
|
343 | self.drop_io_state(POLLOUT) | |
350 |
|
344 | |||
351 | def _handle_err(self): |
|
345 | def _handle_err(self): | |
352 | # We don't want to let this go silently, so eventually we should log. |
|
346 | # We don't want to let this go silently, so eventually we should log. | |
353 | raise zmq.ZMQError() |
|
347 | raise zmq.ZMQError() | |
354 |
|
348 | |||
355 | def _queue_request(self, msg): |
|
349 | def _queue_request(self, msg): | |
356 | self.command_queue.put(msg) |
|
350 | self.command_queue.put(msg) | |
357 | self.add_io_state(POLLOUT) |
|
351 | self.add_io_state(POLLOUT) | |
358 |
|
352 | |||
359 |
|
353 | |||
360 | class SubSocketChannel(ZmqSocketChannel): |
|
354 | class SubSocketChannel(ZmqSocketChannel): | |
361 | """The SUB channel which listens for messages that the kernel publishes. |
|
355 | """The SUB channel which listens for messages that the kernel publishes. | |
362 | """ |
|
356 | """ | |
363 |
|
357 | |||
364 | def __init__(self, context, session, address): |
|
358 | def __init__(self, context, session, address): | |
365 | super(SubSocketChannel, self).__init__(context, session, address) |
|
359 | super(SubSocketChannel, self).__init__(context, session, address) | |
366 | self.ioloop = ioloop.IOLoop() |
|
360 | self.ioloop = ioloop.IOLoop() | |
367 |
|
361 | |||
368 | def run(self): |
|
362 | def run(self): | |
369 | """The thread's main activity. Call start() instead.""" |
|
363 | """The thread's main activity. Call start() instead.""" | |
370 | self.socket = self.context.socket(zmq.SUB) |
|
364 | self.socket = self.context.socket(zmq.SUB) | |
371 | self.socket.setsockopt(zmq.SUBSCRIBE,'') |
|
365 | self.socket.setsockopt(zmq.SUBSCRIBE,'') | |
372 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
366 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
373 | self.socket.connect('tcp://%s:%i' % self.address) |
|
367 | self.socket.connect('tcp://%s:%i' % self.address) | |
374 | self.iostate = POLLIN|POLLERR |
|
368 | self.iostate = POLLIN|POLLERR | |
375 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
369 | self.ioloop.add_handler(self.socket, self._handle_events, | |
376 | self.iostate) |
|
370 | self.iostate) | |
377 | self.ioloop.start() |
|
371 | self.ioloop.start() | |
378 |
|
372 | |||
379 | def stop(self): |
|
373 | def stop(self): | |
380 | self.ioloop.stop() |
|
374 | self.ioloop.stop() | |
381 | super(SubSocketChannel, self).stop() |
|
375 | super(SubSocketChannel, self).stop() | |
382 |
|
376 | |||
383 | def call_handlers(self, msg): |
|
377 | def call_handlers(self, msg): | |
384 | """This method is called in the ioloop thread when a message arrives. |
|
378 | """This method is called in the ioloop thread when a message arrives. | |
385 |
|
379 | |||
386 | Subclasses should override this method to handle incoming messages. |
|
380 | Subclasses should override this method to handle incoming messages. | |
387 | It is important to remember that this method is called in the thread |
|
381 | It is important to remember that this method is called in the thread | |
388 | so that some logic must be done to ensure that the application leve |
|
382 | so that some logic must be done to ensure that the application leve | |
389 | handlers are called in the application thread. |
|
383 | handlers are called in the application thread. | |
390 | """ |
|
384 | """ | |
391 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
385 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
392 |
|
386 | |||
393 | def flush(self, timeout=1.0): |
|
387 | def flush(self, timeout=1.0): | |
394 | """Immediately processes all pending messages on the SUB channel. |
|
388 | """Immediately processes all pending messages on the SUB channel. | |
395 |
|
389 | |||
396 | Callers should use this method to ensure that :method:`call_handlers` |
|
390 | Callers should use this method to ensure that :method:`call_handlers` | |
397 | has been called for all messages that have been received on the |
|
391 | has been called for all messages that have been received on the | |
398 | 0MQ SUB socket of this channel. |
|
392 | 0MQ SUB socket of this channel. | |
399 |
|
393 | |||
400 | This method is thread safe. |
|
394 | This method is thread safe. | |
401 |
|
395 | |||
402 | Parameters |
|
396 | Parameters | |
403 | ---------- |
|
397 | ---------- | |
404 | timeout : float, optional |
|
398 | timeout : float, optional | |
405 | The maximum amount of time to spend flushing, in seconds. The |
|
399 | The maximum amount of time to spend flushing, in seconds. The | |
406 | default is one second. |
|
400 | default is one second. | |
407 | """ |
|
401 | """ | |
408 | # We do the IOLoop callback process twice to ensure that the IOLoop |
|
402 | # We do the IOLoop callback process twice to ensure that the IOLoop | |
409 | # gets to perform at least one full poll. |
|
403 | # gets to perform at least one full poll. | |
410 | stop_time = time.time() + timeout |
|
404 | stop_time = time.time() + timeout | |
411 | for i in xrange(2): |
|
405 | for i in xrange(2): | |
412 | self._flushed = False |
|
406 | self._flushed = False | |
413 | self.ioloop.add_callback(self._flush) |
|
407 | self.ioloop.add_callback(self._flush) | |
414 | while not self._flushed and time.time() < stop_time: |
|
408 | while not self._flushed and time.time() < stop_time: | |
415 | time.sleep(0.01) |
|
409 | time.sleep(0.01) | |
416 |
|
410 | |||
417 | def _handle_events(self, socket, events): |
|
411 | def _handle_events(self, socket, events): | |
418 | # Turn on and off POLLOUT depending on if we have made a request |
|
412 | # Turn on and off POLLOUT depending on if we have made a request | |
419 | if events & POLLERR: |
|
413 | if events & POLLERR: | |
420 | self._handle_err() |
|
414 | self._handle_err() | |
421 | if events & POLLIN: |
|
415 | if events & POLLIN: | |
422 | self._handle_recv() |
|
416 | self._handle_recv() | |
423 |
|
417 | |||
424 | def _handle_err(self): |
|
418 | def _handle_err(self): | |
425 | # We don't want to let this go silently, so eventually we should log. |
|
419 | # We don't want to let this go silently, so eventually we should log. | |
426 | raise zmq.ZMQError() |
|
420 | raise zmq.ZMQError() | |
427 |
|
421 | |||
428 | def _handle_recv(self): |
|
422 | def _handle_recv(self): | |
429 | # Get all of the messages we can |
|
423 | # Get all of the messages we can | |
430 | while True: |
|
424 | while True: | |
431 | try: |
|
425 | try: | |
432 | ident,msg = self.session.recv(self.socket) |
|
426 | ident,msg = self.session.recv(self.socket) | |
433 | except zmq.ZMQError: |
|
427 | except zmq.ZMQError: | |
434 | # Check the errno? |
|
428 | # Check the errno? | |
435 | # Will this trigger POLLERR? |
|
429 | # Will this trigger POLLERR? | |
436 | break |
|
430 | break | |
437 | else: |
|
431 | else: | |
438 | if msg is None: |
|
432 | if msg is None: | |
439 | break |
|
433 | break | |
440 | self.call_handlers(msg) |
|
434 | self.call_handlers(msg) | |
441 |
|
435 | |||
442 | def _flush(self): |
|
436 | def _flush(self): | |
443 | """Callback for :method:`self.flush`.""" |
|
437 | """Callback for :method:`self.flush`.""" | |
444 | self._flushed = True |
|
438 | self._flushed = True | |
445 |
|
439 | |||
446 |
|
440 | |||
447 | class RepSocketChannel(ZmqSocketChannel): |
|
441 | class RepSocketChannel(ZmqSocketChannel): | |
448 | """A reply channel to handle raw_input requests that the kernel makes.""" |
|
442 | """A reply channel to handle raw_input requests that the kernel makes.""" | |
449 |
|
443 | |||
450 | msg_queue = None |
|
444 | msg_queue = None | |
451 |
|
445 | |||
452 | def __init__(self, context, session, address): |
|
446 | def __init__(self, context, session, address): | |
453 | super(RepSocketChannel, self).__init__(context, session, address) |
|
447 | super(RepSocketChannel, self).__init__(context, session, address) | |
454 | self.ioloop = ioloop.IOLoop() |
|
448 | self.ioloop = ioloop.IOLoop() | |
455 | self.msg_queue = Queue() |
|
449 | self.msg_queue = Queue() | |
456 |
|
450 | |||
457 | def run(self): |
|
451 | def run(self): | |
458 | """The thread's main activity. Call start() instead.""" |
|
452 | """The thread's main activity. Call start() instead.""" | |
459 | self.socket = self.context.socket(zmq.XREQ) |
|
453 | self.socket = self.context.socket(zmq.XREQ) | |
460 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
454 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
461 | self.socket.connect('tcp://%s:%i' % self.address) |
|
455 | self.socket.connect('tcp://%s:%i' % self.address) | |
462 | self.iostate = POLLERR|POLLIN |
|
456 | self.iostate = POLLERR|POLLIN | |
463 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
457 | self.ioloop.add_handler(self.socket, self._handle_events, | |
464 | self.iostate) |
|
458 | self.iostate) | |
465 | self.ioloop.start() |
|
459 | self.ioloop.start() | |
466 |
|
460 | |||
467 | def stop(self): |
|
461 | def stop(self): | |
468 | self.ioloop.stop() |
|
462 | self.ioloop.stop() | |
469 | super(RepSocketChannel, self).stop() |
|
463 | super(RepSocketChannel, self).stop() | |
470 |
|
464 | |||
471 | def call_handlers(self, msg): |
|
465 | def call_handlers(self, msg): | |
472 | """This method is called in the ioloop thread when a message arrives. |
|
466 | """This method is called in the ioloop thread when a message arrives. | |
473 |
|
467 | |||
474 | Subclasses should override this method to handle incoming messages. |
|
468 | Subclasses should override this method to handle incoming messages. | |
475 | It is important to remember that this method is called in the thread |
|
469 | It is important to remember that this method is called in the thread | |
476 | so that some logic must be done to ensure that the application leve |
|
470 | so that some logic must be done to ensure that the application leve | |
477 | handlers are called in the application thread. |
|
471 | handlers are called in the application thread. | |
478 | """ |
|
472 | """ | |
479 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
473 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
480 |
|
474 | |||
481 | def input(self, string): |
|
475 | def input(self, string): | |
482 | """Send a string of raw input to the kernel.""" |
|
476 | """Send a string of raw input to the kernel.""" | |
483 | content = dict(value=string) |
|
477 | content = dict(value=string) | |
484 | msg = self.session.msg('input_reply', content) |
|
478 | msg = self.session.msg('input_reply', content) | |
485 | self._queue_reply(msg) |
|
479 | self._queue_reply(msg) | |
486 |
|
480 | |||
487 | def _handle_events(self, socket, events): |
|
481 | def _handle_events(self, socket, events): | |
488 | if events & POLLERR: |
|
482 | if events & POLLERR: | |
489 | self._handle_err() |
|
483 | self._handle_err() | |
490 | if events & POLLOUT: |
|
484 | if events & POLLOUT: | |
491 | self._handle_send() |
|
485 | self._handle_send() | |
492 | if events & POLLIN: |
|
486 | if events & POLLIN: | |
493 | self._handle_recv() |
|
487 | self._handle_recv() | |
494 |
|
488 | |||
495 | def _handle_recv(self): |
|
489 | def _handle_recv(self): | |
496 | ident,msg = self.session.recv(self.socket, 0) |
|
490 | ident,msg = self.session.recv(self.socket, 0) | |
497 | self.call_handlers(msg) |
|
491 | self.call_handlers(msg) | |
498 |
|
492 | |||
499 | def _handle_send(self): |
|
493 | def _handle_send(self): | |
500 | try: |
|
494 | try: | |
501 | msg = self.msg_queue.get(False) |
|
495 | msg = self.msg_queue.get(False) | |
502 | except Empty: |
|
496 | except Empty: | |
503 | pass |
|
497 | pass | |
504 | else: |
|
498 | else: | |
505 | self.session.send(self.socket,msg) |
|
499 | self.session.send(self.socket,msg) | |
506 | if self.msg_queue.empty(): |
|
500 | if self.msg_queue.empty(): | |
507 | self.drop_io_state(POLLOUT) |
|
501 | self.drop_io_state(POLLOUT) | |
508 |
|
502 | |||
509 | def _handle_err(self): |
|
503 | def _handle_err(self): | |
510 | # We don't want to let this go silently, so eventually we should log. |
|
504 | # We don't want to let this go silently, so eventually we should log. | |
511 | raise zmq.ZMQError() |
|
505 | raise zmq.ZMQError() | |
512 |
|
506 | |||
513 | def _queue_reply(self, msg): |
|
507 | def _queue_reply(self, msg): | |
514 | self.msg_queue.put(msg) |
|
508 | self.msg_queue.put(msg) | |
515 | self.add_io_state(POLLOUT) |
|
509 | self.add_io_state(POLLOUT) | |
516 |
|
510 | |||
517 |
|
511 | |||
518 | class HBSocketChannel(ZmqSocketChannel): |
|
512 | class HBSocketChannel(ZmqSocketChannel): | |
519 | """The heartbeat channel which monitors the kernel heartbeat. |
|
513 | """The heartbeat channel which monitors the kernel heartbeat. | |
520 |
|
514 | |||
521 | Note that the heartbeat channel is paused by default. As long as you start |
|
515 | Note that the heartbeat channel is paused by default. As long as you start | |
522 | this channel, the kernel manager will ensure that it is paused and un-paused |
|
516 | this channel, the kernel manager will ensure that it is paused and un-paused | |
523 | as appropriate. |
|
517 | as appropriate. | |
524 | """ |
|
518 | """ | |
525 |
|
519 | |||
526 | time_to_dead = 3.0 |
|
520 | time_to_dead = 3.0 | |
527 | socket = None |
|
521 | socket = None | |
528 | poller = None |
|
522 | poller = None | |
529 | _running = None |
|
523 | _running = None | |
530 | _pause = None |
|
524 | _pause = None | |
531 |
|
525 | |||
532 | def __init__(self, context, session, address): |
|
526 | def __init__(self, context, session, address): | |
533 | super(HBSocketChannel, self).__init__(context, session, address) |
|
527 | super(HBSocketChannel, self).__init__(context, session, address) | |
534 | self._running = False |
|
528 | self._running = False | |
535 | self._pause = True |
|
529 | self._pause = True | |
536 |
|
530 | |||
537 | def _create_socket(self): |
|
531 | def _create_socket(self): | |
538 | self.socket = self.context.socket(zmq.REQ) |
|
532 | self.socket = self.context.socket(zmq.REQ) | |
539 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
533 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
540 | self.socket.connect('tcp://%s:%i' % self.address) |
|
534 | self.socket.connect('tcp://%s:%i' % self.address) | |
541 | self.poller = zmq.Poller() |
|
535 | self.poller = zmq.Poller() | |
542 | self.poller.register(self.socket, zmq.POLLIN) |
|
536 | self.poller.register(self.socket, zmq.POLLIN) | |
543 |
|
537 | |||
544 | def run(self): |
|
538 | def run(self): | |
545 | """The thread's main activity. Call start() instead.""" |
|
539 | """The thread's main activity. Call start() instead.""" | |
546 | self._create_socket() |
|
540 | self._create_socket() | |
547 | self._running = True |
|
541 | self._running = True | |
548 | while self._running: |
|
542 | while self._running: | |
549 | if self._pause: |
|
543 | if self._pause: | |
550 | time.sleep(self.time_to_dead) |
|
544 | time.sleep(self.time_to_dead) | |
551 | else: |
|
545 | else: | |
552 | since_last_heartbeat = 0.0 |
|
546 | since_last_heartbeat = 0.0 | |
553 | request_time = time.time() |
|
547 | request_time = time.time() | |
554 | try: |
|
548 | try: | |
555 | #io.rprint('Ping from HB channel') # dbg |
|
549 | #io.rprint('Ping from HB channel') # dbg | |
556 | self.socket.send(b'ping') |
|
550 | self.socket.send(b'ping') | |
557 | except zmq.ZMQError, e: |
|
551 | except zmq.ZMQError, e: | |
558 | #io.rprint('*** HB Error:', e) # dbg |
|
552 | #io.rprint('*** HB Error:', e) # dbg | |
559 | if e.errno == zmq.EFSM: |
|
553 | if e.errno == zmq.EFSM: | |
560 | #io.rprint('sleep...', self.time_to_dead) # dbg |
|
554 | #io.rprint('sleep...', self.time_to_dead) # dbg | |
561 | time.sleep(self.time_to_dead) |
|
555 | time.sleep(self.time_to_dead) | |
562 | self._create_socket() |
|
556 | self._create_socket() | |
563 | else: |
|
557 | else: | |
564 | raise |
|
558 | raise | |
565 | else: |
|
559 | else: | |
566 | while True: |
|
560 | while True: | |
567 | try: |
|
561 | try: | |
568 | self.socket.recv(zmq.NOBLOCK) |
|
562 | self.socket.recv(zmq.NOBLOCK) | |
569 | except zmq.ZMQError, e: |
|
563 | except zmq.ZMQError, e: | |
570 | #io.rprint('*** HB Error 2:', e) # dbg |
|
564 | #io.rprint('*** HB Error 2:', e) # dbg | |
571 | if e.errno == zmq.EAGAIN: |
|
565 | if e.errno == zmq.EAGAIN: | |
572 | before_poll = time.time() |
|
566 | before_poll = time.time() | |
573 | until_dead = self.time_to_dead - (before_poll - |
|
567 | until_dead = self.time_to_dead - (before_poll - | |
574 | request_time) |
|
568 | request_time) | |
575 |
|
569 | |||
576 | # When the return value of poll() is an empty |
|
570 | # When the return value of poll() is an empty | |
577 | # list, that is when things have gone wrong |
|
571 | # list, that is when things have gone wrong | |
578 | # (zeromq bug). As long as it is not an empty |
|
572 | # (zeromq bug). As long as it is not an empty | |
579 | # list, poll is working correctly even if it |
|
573 | # list, poll is working correctly even if it | |
580 | # returns quickly. Note: poll timeout is in |
|
574 | # returns quickly. Note: poll timeout is in | |
581 | # milliseconds. |
|
575 | # milliseconds. | |
582 | self.poller.poll(1000*until_dead) |
|
576 | self.poller.poll(1000*until_dead) | |
583 |
|
577 | |||
584 | since_last_heartbeat = time.time()-request_time |
|
578 | since_last_heartbeat = time.time()-request_time | |
585 | if since_last_heartbeat > self.time_to_dead: |
|
579 | if since_last_heartbeat > self.time_to_dead: | |
586 | self.call_handlers(since_last_heartbeat) |
|
580 | self.call_handlers(since_last_heartbeat) | |
587 | break |
|
581 | break | |
588 | else: |
|
582 | else: | |
589 | # FIXME: We should probably log this instead. |
|
583 | # FIXME: We should probably log this instead. | |
590 | raise |
|
584 | raise | |
591 | else: |
|
585 | else: | |
592 | until_dead = self.time_to_dead - (time.time() - |
|
586 | until_dead = self.time_to_dead - (time.time() - | |
593 | request_time) |
|
587 | request_time) | |
594 | if until_dead > 0.0: |
|
588 | if until_dead > 0.0: | |
595 | #io.rprint('sleep...', self.time_to_dead) # dbg |
|
589 | #io.rprint('sleep...', self.time_to_dead) # dbg | |
596 | time.sleep(until_dead) |
|
590 | time.sleep(until_dead) | |
597 | break |
|
591 | break | |
598 |
|
592 | |||
599 | def pause(self): |
|
593 | def pause(self): | |
600 | """Pause the heartbeat.""" |
|
594 | """Pause the heartbeat.""" | |
601 | self._pause = True |
|
595 | self._pause = True | |
602 |
|
596 | |||
603 | def unpause(self): |
|
597 | def unpause(self): | |
604 | """Unpause the heartbeat.""" |
|
598 | """Unpause the heartbeat.""" | |
605 | self._pause = False |
|
599 | self._pause = False | |
606 |
|
600 | |||
607 | def is_beating(self): |
|
601 | def is_beating(self): | |
608 | """Is the heartbeat running and not paused.""" |
|
602 | """Is the heartbeat running and not paused.""" | |
609 | if self.is_alive() and not self._pause: |
|
603 | if self.is_alive() and not self._pause: | |
610 | return True |
|
604 | return True | |
611 | else: |
|
605 | else: | |
612 | return False |
|
606 | return False | |
613 |
|
607 | |||
614 | def stop(self): |
|
608 | def stop(self): | |
615 | self._running = False |
|
609 | self._running = False | |
616 | super(HBSocketChannel, self).stop() |
|
610 | super(HBSocketChannel, self).stop() | |
617 |
|
611 | |||
618 | def call_handlers(self, since_last_heartbeat): |
|
612 | def call_handlers(self, since_last_heartbeat): | |
619 | """This method is called in the ioloop thread when a message arrives. |
|
613 | """This method is called in the ioloop thread when a message arrives. | |
620 |
|
614 | |||
621 | Subclasses should override this method to handle incoming messages. |
|
615 | Subclasses should override this method to handle incoming messages. | |
622 | It is important to remember that this method is called in the thread |
|
616 | It is important to remember that this method is called in the thread | |
623 | so that some logic must be done to ensure that the application leve |
|
617 | so that some logic must be done to ensure that the application leve | |
624 | handlers are called in the application thread. |
|
618 | handlers are called in the application thread. | |
625 | """ |
|
619 | """ | |
626 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
620 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
627 |
|
621 | |||
628 |
|
622 | |||
629 | #----------------------------------------------------------------------------- |
|
623 | #----------------------------------------------------------------------------- | |
630 | # Main kernel manager class |
|
624 | # Main kernel manager class | |
631 | #----------------------------------------------------------------------------- |
|
625 | #----------------------------------------------------------------------------- | |
632 |
|
626 | |||
633 | class KernelManager(HasTraits): |
|
627 | class KernelManager(HasTraits): | |
634 | """ Manages a kernel for a frontend. |
|
628 | """ Manages a kernel for a frontend. | |
635 |
|
629 | |||
636 | The SUB channel is for the frontend to receive messages published by the |
|
630 | The SUB channel is for the frontend to receive messages published by the | |
637 | kernel. |
|
631 | kernel. | |
638 |
|
632 | |||
639 | The REQ channel is for the frontend to make requests of the kernel. |
|
633 | The REQ channel is for the frontend to make requests of the kernel. | |
640 |
|
634 | |||
641 | The REP channel is for the kernel to request stdin (raw_input) from the |
|
635 | The REP channel is for the kernel to request stdin (raw_input) from the | |
642 | frontend. |
|
636 | frontend. | |
643 | """ |
|
637 | """ | |
644 | # The PyZMQ Context to use for communication with the kernel. |
|
638 | # The PyZMQ Context to use for communication with the kernel. | |
645 | context = Instance(zmq.Context,(),{}) |
|
639 | context = Instance(zmq.Context,(),{}) | |
646 |
|
640 | |||
647 | # The Session to use for communication with the kernel. |
|
641 | # The Session to use for communication with the kernel. | |
648 | session = Instance(Session,(),{}) |
|
642 | session = Instance(Session,(),{}) | |
649 |
|
643 | |||
650 | # The kernel process with which the KernelManager is communicating. |
|
644 | # The kernel process with which the KernelManager is communicating. | |
651 | kernel = Instance(Popen) |
|
645 | kernel = Instance(Popen) | |
652 |
|
646 | |||
653 | # The addresses for the communication channels. |
|
647 | # The addresses for the communication channels. | |
654 | xreq_address = TCPAddress((LOCALHOST, 0)) |
|
648 | xreq_address = TCPAddress((LOCALHOST, 0)) | |
655 | sub_address = TCPAddress((LOCALHOST, 0)) |
|
649 | sub_address = TCPAddress((LOCALHOST, 0)) | |
656 | rep_address = TCPAddress((LOCALHOST, 0)) |
|
650 | rep_address = TCPAddress((LOCALHOST, 0)) | |
657 | hb_address = TCPAddress((LOCALHOST, 0)) |
|
651 | hb_address = TCPAddress((LOCALHOST, 0)) | |
658 |
|
652 | |||
659 | # The classes to use for the various channels. |
|
653 | # The classes to use for the various channels. | |
660 | xreq_channel_class = Type(XReqSocketChannel) |
|
654 | xreq_channel_class = Type(XReqSocketChannel) | |
661 | sub_channel_class = Type(SubSocketChannel) |
|
655 | sub_channel_class = Type(SubSocketChannel) | |
662 | rep_channel_class = Type(RepSocketChannel) |
|
656 | rep_channel_class = Type(RepSocketChannel) | |
663 | hb_channel_class = Type(HBSocketChannel) |
|
657 | hb_channel_class = Type(HBSocketChannel) | |
664 |
|
658 | |||
665 | # Protected traits. |
|
659 | # Protected traits. | |
666 | _launch_args = Any |
|
660 | _launch_args = Any | |
667 | _xreq_channel = Any |
|
661 | _xreq_channel = Any | |
668 | _sub_channel = Any |
|
662 | _sub_channel = Any | |
669 | _rep_channel = Any |
|
663 | _rep_channel = Any | |
670 | _hb_channel = Any |
|
664 | _hb_channel = Any | |
671 |
|
665 | |||
672 | def __init__(self, **kwargs): |
|
666 | def __init__(self, **kwargs): | |
673 | super(KernelManager, self).__init__(**kwargs) |
|
667 | super(KernelManager, self).__init__(**kwargs) | |
674 | # Uncomment this to try closing the context. |
|
668 | # Uncomment this to try closing the context. | |
675 | # atexit.register(self.context.close) |
|
669 | # atexit.register(self.context.close) | |
676 |
|
670 | |||
677 | #-------------------------------------------------------------------------- |
|
671 | #-------------------------------------------------------------------------- | |
678 | # Channel management methods: |
|
672 | # Channel management methods: | |
679 | #-------------------------------------------------------------------------- |
|
673 | #-------------------------------------------------------------------------- | |
680 |
|
674 | |||
681 | def start_channels(self, xreq=True, sub=True, rep=True, hb=True): |
|
675 | def start_channels(self, xreq=True, sub=True, rep=True, hb=True): | |
682 | """Starts the channels for this kernel. |
|
676 | """Starts the channels for this kernel. | |
683 |
|
677 | |||
684 | This will create the channels if they do not exist and then start |
|
678 | This will create the channels if they do not exist and then start | |
685 | them. If port numbers of 0 are being used (random ports) then you |
|
679 | them. If port numbers of 0 are being used (random ports) then you | |
686 | must first call :method:`start_kernel`. If the channels have been |
|
680 | must first call :method:`start_kernel`. If the channels have been | |
687 | stopped and you call this, :class:`RuntimeError` will be raised. |
|
681 | stopped and you call this, :class:`RuntimeError` will be raised. | |
688 | """ |
|
682 | """ | |
689 | if xreq: |
|
683 | if xreq: | |
690 | self.xreq_channel.start() |
|
684 | self.xreq_channel.start() | |
691 | if sub: |
|
685 | if sub: | |
692 | self.sub_channel.start() |
|
686 | self.sub_channel.start() | |
693 | if rep: |
|
687 | if rep: | |
694 | self.rep_channel.start() |
|
688 | self.rep_channel.start() | |
695 | if hb: |
|
689 | if hb: | |
696 | self.hb_channel.start() |
|
690 | self.hb_channel.start() | |
697 |
|
691 | |||
698 | def stop_channels(self): |
|
692 | def stop_channels(self): | |
699 | """Stops all the running channels for this kernel. |
|
693 | """Stops all the running channels for this kernel. | |
700 | """ |
|
694 | """ | |
701 | if self.xreq_channel.is_alive(): |
|
695 | if self.xreq_channel.is_alive(): | |
702 | self.xreq_channel.stop() |
|
696 | self.xreq_channel.stop() | |
703 | if self.sub_channel.is_alive(): |
|
697 | if self.sub_channel.is_alive(): | |
704 | self.sub_channel.stop() |
|
698 | self.sub_channel.stop() | |
705 | if self.rep_channel.is_alive(): |
|
699 | if self.rep_channel.is_alive(): | |
706 | self.rep_channel.stop() |
|
700 | self.rep_channel.stop() | |
707 | if self.hb_channel.is_alive(): |
|
701 | if self.hb_channel.is_alive(): | |
708 | self.hb_channel.stop() |
|
702 | self.hb_channel.stop() | |
709 |
|
703 | |||
710 | @property |
|
704 | @property | |
711 | def channels_running(self): |
|
705 | def channels_running(self): | |
712 | """Are any of the channels created and running?""" |
|
706 | """Are any of the channels created and running?""" | |
713 | return (self.xreq_channel.is_alive() or self.sub_channel.is_alive() or |
|
707 | return (self.xreq_channel.is_alive() or self.sub_channel.is_alive() or | |
714 | self.rep_channel.is_alive() or self.hb_channel.is_alive()) |
|
708 | self.rep_channel.is_alive() or self.hb_channel.is_alive()) | |
715 |
|
709 | |||
716 | #-------------------------------------------------------------------------- |
|
710 | #-------------------------------------------------------------------------- | |
717 | # Kernel process management methods: |
|
711 | # Kernel process management methods: | |
718 | #-------------------------------------------------------------------------- |
|
712 | #-------------------------------------------------------------------------- | |
719 |
|
713 | |||
720 | def start_kernel(self, **kw): |
|
714 | def start_kernel(self, **kw): | |
721 | """Starts a kernel process and configures the manager to use it. |
|
715 | """Starts a kernel process and configures the manager to use it. | |
722 |
|
716 | |||
723 | If random ports (port=0) are being used, this method must be called |
|
717 | If random ports (port=0) are being used, this method must be called | |
724 | before the channels are created. |
|
718 | before the channels are created. | |
725 |
|
719 | |||
726 | Parameters: |
|
720 | Parameters: | |
727 | ----------- |
|
721 | ----------- | |
728 | ipython : bool, optional (default True) |
|
722 | ipython : bool, optional (default True) | |
729 | Whether to use an IPython kernel instead of a plain Python kernel. |
|
723 | Whether to use an IPython kernel instead of a plain Python kernel. | |
730 | """ |
|
724 | """ | |
731 | xreq, sub, rep, hb = self.xreq_address, self.sub_address, \ |
|
725 | xreq, sub, rep, hb = self.xreq_address, self.sub_address, \ | |
732 | self.rep_address, self.hb_address |
|
726 | self.rep_address, self.hb_address | |
733 | if xreq[0] not in LOCAL_IPS or sub[0] not in LOCAL_IPS or \ |
|
727 | if xreq[0] not in LOCAL_IPS or sub[0] not in LOCAL_IPS or \ | |
734 | rep[0] not in LOCAL_IPS or hb[0] not in LOCAL_IPS: |
|
728 | rep[0] not in LOCAL_IPS or hb[0] not in LOCAL_IPS: | |
735 | raise RuntimeError("Can only launch a kernel on a local interface. " |
|
729 | raise RuntimeError("Can only launch a kernel on a local interface. " | |
736 | "Make sure that the '*_address' attributes are " |
|
730 | "Make sure that the '*_address' attributes are " | |
737 | "configured properly. " |
|
731 | "configured properly. " | |
738 | "Currently valid addresses are: %s"%LOCAL_IPS |
|
732 | "Currently valid addresses are: %s"%LOCAL_IPS | |
739 | ) |
|
733 | ) | |
740 |
|
734 | |||
741 | self._launch_args = kw.copy() |
|
735 | self._launch_args = kw.copy() | |
742 | if kw.pop('ipython', True): |
|
736 | if kw.pop('ipython', True): | |
743 | from ipkernel import launch_kernel |
|
737 | from ipkernel import launch_kernel | |
744 | else: |
|
738 | else: | |
745 | from pykernel import launch_kernel |
|
739 | from pykernel import launch_kernel | |
746 | self.kernel, xrep, pub, req, _hb = launch_kernel( |
|
740 | self.kernel, xrep, pub, req, _hb = launch_kernel( | |
747 | xrep_port=xreq[1], pub_port=sub[1], |
|
741 | xrep_port=xreq[1], pub_port=sub[1], | |
748 | req_port=rep[1], hb_port=hb[1], **kw) |
|
742 | req_port=rep[1], hb_port=hb[1], **kw) | |
749 | self.xreq_address = (xreq[0], xrep) |
|
743 | self.xreq_address = (xreq[0], xrep) | |
750 | self.sub_address = (sub[0], pub) |
|
744 | self.sub_address = (sub[0], pub) | |
751 | self.rep_address = (rep[0], req) |
|
745 | self.rep_address = (rep[0], req) | |
752 | self.hb_address = (hb[0], _hb) |
|
746 | self.hb_address = (hb[0], _hb) | |
753 |
|
747 | |||
754 | def shutdown_kernel(self, restart=False): |
|
748 | def shutdown_kernel(self, restart=False): | |
755 | """ Attempts to the stop the kernel process cleanly. If the kernel |
|
749 | """ Attempts to the stop the kernel process cleanly. If the kernel | |
756 | cannot be stopped, it is killed, if possible. |
|
750 | cannot be stopped, it is killed, if possible. | |
757 | """ |
|
751 | """ | |
758 | # FIXME: Shutdown does not work on Windows due to ZMQ errors! |
|
752 | # FIXME: Shutdown does not work on Windows due to ZMQ errors! | |
759 | if sys.platform == 'win32': |
|
753 | if sys.platform == 'win32': | |
760 | self.kill_kernel() |
|
754 | self.kill_kernel() | |
761 | return |
|
755 | return | |
762 |
|
756 | |||
763 | # Pause the heart beat channel if it exists. |
|
757 | # Pause the heart beat channel if it exists. | |
764 | if self._hb_channel is not None: |
|
758 | if self._hb_channel is not None: | |
765 | self._hb_channel.pause() |
|
759 | self._hb_channel.pause() | |
766 |
|
760 | |||
767 | # Don't send any additional kernel kill messages immediately, to give |
|
761 | # Don't send any additional kernel kill messages immediately, to give | |
768 | # the kernel a chance to properly execute shutdown actions. Wait for at |
|
762 | # the kernel a chance to properly execute shutdown actions. Wait for at | |
769 | # most 1s, checking every 0.1s. |
|
763 | # most 1s, checking every 0.1s. | |
770 | self.xreq_channel.shutdown(restart=restart) |
|
764 | self.xreq_channel.shutdown(restart=restart) | |
771 | for i in range(10): |
|
765 | for i in range(10): | |
772 | if self.is_alive: |
|
766 | if self.is_alive: | |
773 | time.sleep(0.1) |
|
767 | time.sleep(0.1) | |
774 | else: |
|
768 | else: | |
775 | break |
|
769 | break | |
776 | else: |
|
770 | else: | |
777 | # OK, we've waited long enough. |
|
771 | # OK, we've waited long enough. | |
778 | if self.has_kernel: |
|
772 | if self.has_kernel: | |
779 | self.kill_kernel() |
|
773 | self.kill_kernel() | |
780 |
|
774 | |||
781 | def restart_kernel(self, now=False): |
|
775 | def restart_kernel(self, now=False): | |
782 | """Restarts a kernel with the same arguments that were used to launch |
|
776 | """Restarts a kernel with the same arguments that were used to launch | |
783 | it. If the old kernel was launched with random ports, the same ports |
|
777 | it. If the old kernel was launched with random ports, the same ports | |
784 | will be used for the new kernel. |
|
778 | will be used for the new kernel. | |
785 |
|
779 | |||
786 | Parameters |
|
780 | Parameters | |
787 | ---------- |
|
781 | ---------- | |
788 | now : bool, optional |
|
782 | now : bool, optional | |
789 | If True, the kernel is forcefully restarted *immediately*, without |
|
783 | If True, the kernel is forcefully restarted *immediately*, without | |
790 | having a chance to do any cleanup action. Otherwise the kernel is |
|
784 | having a chance to do any cleanup action. Otherwise the kernel is | |
791 | given 1s to clean up before a forceful restart is issued. |
|
785 | given 1s to clean up before a forceful restart is issued. | |
792 |
|
786 | |||
793 | In all cases the kernel is restarted, the only difference is whether |
|
787 | In all cases the kernel is restarted, the only difference is whether | |
794 | it is given a chance to perform a clean shutdown or not. |
|
788 | it is given a chance to perform a clean shutdown or not. | |
795 | """ |
|
789 | """ | |
796 | if self._launch_args is None: |
|
790 | if self._launch_args is None: | |
797 | raise RuntimeError("Cannot restart the kernel. " |
|
791 | raise RuntimeError("Cannot restart the kernel. " | |
798 | "No previous call to 'start_kernel'.") |
|
792 | "No previous call to 'start_kernel'.") | |
799 | else: |
|
793 | else: | |
800 | if self.has_kernel: |
|
794 | if self.has_kernel: | |
801 | if now: |
|
795 | if now: | |
802 | self.kill_kernel() |
|
796 | self.kill_kernel() | |
803 | else: |
|
797 | else: | |
804 | self.shutdown_kernel(restart=True) |
|
798 | self.shutdown_kernel(restart=True) | |
805 | self.start_kernel(**self._launch_args) |
|
799 | self.start_kernel(**self._launch_args) | |
806 |
|
800 | |||
807 | # FIXME: Messages get dropped in Windows due to probable ZMQ bug |
|
801 | # FIXME: Messages get dropped in Windows due to probable ZMQ bug | |
808 | # unless there is some delay here. |
|
802 | # unless there is some delay here. | |
809 | if sys.platform == 'win32': |
|
803 | if sys.platform == 'win32': | |
810 | time.sleep(0.2) |
|
804 | time.sleep(0.2) | |
811 |
|
805 | |||
812 | @property |
|
806 | @property | |
813 | def has_kernel(self): |
|
807 | def has_kernel(self): | |
814 | """Returns whether a kernel process has been specified for the kernel |
|
808 | """Returns whether a kernel process has been specified for the kernel | |
815 | manager. |
|
809 | manager. | |
816 | """ |
|
810 | """ | |
817 | return self.kernel is not None |
|
811 | return self.kernel is not None | |
818 |
|
812 | |||
819 | def kill_kernel(self): |
|
813 | def kill_kernel(self): | |
820 | """ Kill the running kernel. """ |
|
814 | """ Kill the running kernel. """ | |
821 | if self.has_kernel: |
|
815 | if self.has_kernel: | |
822 | # Pause the heart beat channel if it exists. |
|
816 | # Pause the heart beat channel if it exists. | |
823 | if self._hb_channel is not None: |
|
817 | if self._hb_channel is not None: | |
824 | self._hb_channel.pause() |
|
818 | self._hb_channel.pause() | |
825 |
|
819 | |||
826 | # Attempt to kill the kernel. |
|
820 | # Attempt to kill the kernel. | |
827 | try: |
|
821 | try: | |
828 | self.kernel.kill() |
|
822 | self.kernel.kill() | |
829 | except OSError, e: |
|
823 | except OSError, e: | |
830 | # In Windows, we will get an Access Denied error if the process |
|
824 | # In Windows, we will get an Access Denied error if the process | |
831 | # has already terminated. Ignore it. |
|
825 | # has already terminated. Ignore it. | |
832 | if not (sys.platform == 'win32' and e.winerror == 5): |
|
826 | if not (sys.platform == 'win32' and e.winerror == 5): | |
833 | raise |
|
827 | raise | |
834 | self.kernel = None |
|
828 | self.kernel = None | |
835 | else: |
|
829 | else: | |
836 | raise RuntimeError("Cannot kill kernel. No kernel is running!") |
|
830 | raise RuntimeError("Cannot kill kernel. No kernel is running!") | |
837 |
|
831 | |||
838 | def interrupt_kernel(self): |
|
832 | def interrupt_kernel(self): | |
839 | """ Interrupts the kernel. Unlike ``signal_kernel``, this operation is |
|
833 | """ Interrupts the kernel. Unlike ``signal_kernel``, this operation is | |
840 | well supported on all platforms. |
|
834 | well supported on all platforms. | |
841 | """ |
|
835 | """ | |
842 | if self.has_kernel: |
|
836 | if self.has_kernel: | |
843 | if sys.platform == 'win32': |
|
837 | if sys.platform == 'win32': | |
844 | from parentpoller import ParentPollerWindows as Poller |
|
838 | from parentpoller import ParentPollerWindows as Poller | |
845 | Poller.send_interrupt(self.kernel.win32_interrupt_event) |
|
839 | Poller.send_interrupt(self.kernel.win32_interrupt_event) | |
846 | else: |
|
840 | else: | |
847 | self.kernel.send_signal(signal.SIGINT) |
|
841 | self.kernel.send_signal(signal.SIGINT) | |
848 | else: |
|
842 | else: | |
849 | raise RuntimeError("Cannot interrupt kernel. No kernel is running!") |
|
843 | raise RuntimeError("Cannot interrupt kernel. No kernel is running!") | |
850 |
|
844 | |||
851 | def signal_kernel(self, signum): |
|
845 | def signal_kernel(self, signum): | |
852 | """ Sends a signal to the kernel. Note that since only SIGTERM is |
|
846 | """ Sends a signal to the kernel. Note that since only SIGTERM is | |
853 | supported on Windows, this function is only useful on Unix systems. |
|
847 | supported on Windows, this function is only useful on Unix systems. | |
854 | """ |
|
848 | """ | |
855 | if self.has_kernel: |
|
849 | if self.has_kernel: | |
856 | self.kernel.send_signal(signum) |
|
850 | self.kernel.send_signal(signum) | |
857 | else: |
|
851 | else: | |
858 | raise RuntimeError("Cannot signal kernel. No kernel is running!") |
|
852 | raise RuntimeError("Cannot signal kernel. No kernel is running!") | |
859 |
|
853 | |||
860 | @property |
|
854 | @property | |
861 | def is_alive(self): |
|
855 | def is_alive(self): | |
862 | """Is the kernel process still running?""" |
|
856 | """Is the kernel process still running?""" | |
863 | # FIXME: not using a heartbeat means this method is broken for any |
|
857 | # FIXME: not using a heartbeat means this method is broken for any | |
864 | # remote kernel, it's only capable of handling local kernels. |
|
858 | # remote kernel, it's only capable of handling local kernels. | |
865 | if self.has_kernel: |
|
859 | if self.has_kernel: | |
866 | if self.kernel.poll() is None: |
|
860 | if self.kernel.poll() is None: | |
867 | return True |
|
861 | return True | |
868 | else: |
|
862 | else: | |
869 | return False |
|
863 | return False | |
870 | else: |
|
864 | else: | |
871 | # We didn't start the kernel with this KernelManager so we don't |
|
865 | # We didn't start the kernel with this KernelManager so we don't | |
872 | # know if it is running. We should use a heartbeat for this case. |
|
866 | # know if it is running. We should use a heartbeat for this case. | |
873 | return True |
|
867 | return True | |
874 |
|
868 | |||
875 | #-------------------------------------------------------------------------- |
|
869 | #-------------------------------------------------------------------------- | |
876 | # Channels used for communication with the kernel: |
|
870 | # Channels used for communication with the kernel: | |
877 | #-------------------------------------------------------------------------- |
|
871 | #-------------------------------------------------------------------------- | |
878 |
|
872 | |||
879 | @property |
|
873 | @property | |
880 | def xreq_channel(self): |
|
874 | def xreq_channel(self): | |
881 | """Get the REQ socket channel object to make requests of the kernel.""" |
|
875 | """Get the REQ socket channel object to make requests of the kernel.""" | |
882 | if self._xreq_channel is None: |
|
876 | if self._xreq_channel is None: | |
883 | self._xreq_channel = self.xreq_channel_class(self.context, |
|
877 | self._xreq_channel = self.xreq_channel_class(self.context, | |
884 | self.session, |
|
878 | self.session, | |
885 | self.xreq_address) |
|
879 | self.xreq_address) | |
886 | return self._xreq_channel |
|
880 | return self._xreq_channel | |
887 |
|
881 | |||
888 | @property |
|
882 | @property | |
889 | def sub_channel(self): |
|
883 | def sub_channel(self): | |
890 | """Get the SUB socket channel object.""" |
|
884 | """Get the SUB socket channel object.""" | |
891 | if self._sub_channel is None: |
|
885 | if self._sub_channel is None: | |
892 | self._sub_channel = self.sub_channel_class(self.context, |
|
886 | self._sub_channel = self.sub_channel_class(self.context, | |
893 | self.session, |
|
887 | self.session, | |
894 | self.sub_address) |
|
888 | self.sub_address) | |
895 | return self._sub_channel |
|
889 | return self._sub_channel | |
896 |
|
890 | |||
897 | @property |
|
891 | @property | |
898 | def rep_channel(self): |
|
892 | def rep_channel(self): | |
899 | """Get the REP socket channel object to handle stdin (raw_input).""" |
|
893 | """Get the REP socket channel object to handle stdin (raw_input).""" | |
900 | if self._rep_channel is None: |
|
894 | if self._rep_channel is None: | |
901 | self._rep_channel = self.rep_channel_class(self.context, |
|
895 | self._rep_channel = self.rep_channel_class(self.context, | |
902 | self.session, |
|
896 | self.session, | |
903 | self.rep_address) |
|
897 | self.rep_address) | |
904 | return self._rep_channel |
|
898 | return self._rep_channel | |
905 |
|
899 | |||
906 | @property |
|
900 | @property | |
907 | def hb_channel(self): |
|
901 | def hb_channel(self): | |
908 | """Get the heartbeat socket channel object to check that the |
|
902 | """Get the heartbeat socket channel object to check that the | |
909 | kernel is alive.""" |
|
903 | kernel is alive.""" | |
910 | if self._hb_channel is None: |
|
904 | if self._hb_channel is None: | |
911 | self._hb_channel = self.hb_channel_class(self.context, |
|
905 | self._hb_channel = self.hb_channel_class(self.context, | |
912 | self.session, |
|
906 | self.session, | |
913 | self.hb_address) |
|
907 | self.hb_address) | |
914 | return self._hb_channel |
|
908 | return self._hb_channel |
General Comments 0
You need to be logged in to leave comments.
Login now