##// END OF EJS Templates
ipython-qtconsole now calls the right function.
Thomas Kluyver -
Show More
@@ -1,581 +1,592 b''
1 1 """ History related magics and functionality """
2 2 #-----------------------------------------------------------------------------
3 3 # Copyright (C) 2010 The IPython Development Team.
4 4 #
5 5 # Distributed under the terms of the BSD License.
6 6 #
7 7 # The full license is in the file COPYING.txt, distributed with this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # Stdlib imports
16 16 import os
17 17 import re
18 18 import sqlite3
19 19
20 20 # Our own packages
21 21 from IPython.config.configurable import Configurable
22 22 import IPython.utils.io
23 23
24 24 from IPython.testing import decorators as testdec
25 25 from IPython.utils.io import ask_yes_no
26 26 from IPython.utils.traitlets import Bool, Dict, Instance, Int, List, Unicode
27 27 from IPython.utils.warn import warn
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes and functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33 class HistoryManager(Configurable):
34 34 """A class to organize all history-related functionality in one place.
35 35 """
36 36 # Public interface
37 37
38 38 # An instance of the IPython shell we are attached to
39 39 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
40 40 # Lists to hold processed and raw history. These start with a blank entry
41 41 # so that we can index them starting from 1
42 42 input_hist_parsed = List([""])
43 43 input_hist_raw = List([""])
44 44 # A list of directories visited during session
45 45 dir_hist = List()
46 46 # A dict of output history, keyed with ints from the shell's execution count
47 47 output_hist = Dict()
48 48 # String holding the path to the history file
49 49 hist_file = Unicode()
50 50 # The SQLite database
51 51 db = Instance(sqlite3.Connection)
52 52 # The number of the current session in the history database
53 53 session_number = Int()
54 54 # Should we log output to the database? (default no)
55 55 db_log_output = Bool(False, config=True)
56 56 # Write to database every x commands (higher values save disk access & power)
57 57 # Values of 1 or less effectively disable caching.
58 58 db_cache_size = Int(0, config=True)
59 59 # The input and output caches
60 60 db_input_cache = List()
61 61 db_output_cache = List()
62 62
63 63 # Private interface
64 64 # Variables used to store the three last inputs from the user. On each new
65 65 # history update, we populate the user's namespace with these, shifted as
66 66 # necessary.
67 67 _i00, _i, _ii, _iii = '','','',''
68 68
69 69 # A set with all forms of the exit command, so that we don't store them in
70 70 # the history (it's annoying to rewind the first entry and land on an exit
71 71 # call).
72 72 _exit_commands = None
73 73
74 74 def __init__(self, shell, config=None):
75 75 """Create a new history manager associated with a shell instance.
76 76 """
77 77 # We need a pointer back to the shell for various tasks.
78 78 super(HistoryManager, self).__init__(shell=shell, config=config)
79 79
80 80 # list of visited directories
81 81 try:
82 82 self.dir_hist = [os.getcwd()]
83 83 except OSError:
84 84 self.dir_hist = []
85 85
86 86 # Now the history file
87 87 if shell.profile:
88 88 histfname = 'history-%s' % shell.profile
89 89 else:
90 90 histfname = 'history'
91 91 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
92 92 self.init_db()
93 93
94 94 self._i00, self._i, self._ii, self._iii = '','','',''
95 95
96 96 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
97 97 '%quit', '%Exit', '%exit'])
98 98
99 99 def init_db(self):
100 100 """Connect to the database and get new session number."""
101 101 self.db = sqlite3.connect(self.hist_file)
102 102 self.db.execute("""CREATE TABLE IF NOT EXISTS history
103 103 (session integer, line integer, source text, source_raw text,
104 104 PRIMARY KEY (session, line))""")
105 105 # Output history is optional, but ensure the table's there so it can be
106 106 # enabled later.
107 107 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
108 108 (session integer, line integer, output text,
109 109 PRIMARY KEY (session, line))""")
110 110 cur = self.db.execute("""SELECT name FROM sqlite_master WHERE
111 111 type='table' AND name='singletons'""")
112 112 if not cur.fetchone():
113 113 self.db.execute("""CREATE TABLE singletons
114 114 (name text PRIMARY KEY, value)""")
115 115 self.db.execute("""INSERT INTO singletons VALUES
116 116 ('session_number', 1)""")
117 117 self.db.commit()
118 118 cur = self.db.execute("""SELECT value FROM singletons WHERE
119 119 name='session_number'""")
120 120 self.session_number = cur.fetchone()[0]
121 121
122 122 #Increment by one for next session.
123 123 self.db.execute("""UPDATE singletons SET value=? WHERE
124 124 name='session_number'""", (self.session_number+1,))
125 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 128 """Get the last n lines from the history database."""
129 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 134 cur = self.db.execute("SELECT session, line, " + toget +\
131 " FROM history ORDER BY session DESC, line DESC LIMIT ?""", (n,))
132 return reversed(cur.fetchall())
135 " FROM "+sqlfrom+" ORDER BY session DESC, line DESC LIMIT ?", (n,))
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 141 def get_hist_search(self, pattern="*", raw=True):
135 142 """Search the database using unix glob-style matching (wildcards * and
136 143 ?, escape using \).
137 144
138 145 Returns
139 146 -------
140 147 An iterator over tuples: (session, line_number, command)
141 148 """
142 149 toget = "source_raw" if raw else source
143 150 return self.db.execute("SELECT session, line, " +toget+ \
144 151 "FROM history WHERE" +toget+ "GLOB ?", (pattern,))
145 152
146 153 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
147 154 """Get input and output history from the current session. Called by
148 155 get_history, and takes similar parameters."""
149 156 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
150 157
151 158 n = len(input_hist)
152 159 if start < 0:
153 160 start += n
154 161 if not stop:
155 162 stop = n
156 163 elif stop < 0:
157 164 stop += n
158 165
159 166 for i in range(start, stop):
160 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 169 else:
163 170 line = input_hist[i]
164 171 yield (0, i, line)
165 172
166 173 def get_history(self, session=0, start=1, stop=None, raw=True,output=False):
167 174 """Retrieve input by session.
168 175
169 176 Parameters
170 177 ----------
171 178 session : int
172 179 Session number to retrieve. The current session is 0, and negative
173 180 numbers count back from current session, so -1 is previous session.
174 181 start : int
175 182 First line to retrieve.
176 183 stop : int
177 184 End of line range (excluded from output itself). If None, retrieve
178 185 to the end of the session.
179 186 raw : bool
180 187 If True, return untranslated input
181 188 output : bool
182 189 If True, attempt to include output. This will be 'real' Python
183 190 objects for the current session, or text reprs from previous
184 191 sessions if db_log_output was enabled at the time. Where no output
185 192 is found, None is used.
186 193
187 194 Returns
188 195 -------
189 196 An iterator over the desired lines. Each line is a 3-tuple, either
190 197 (session, line, input) if output is False, or
191 198 (session, line, (input, output)) if output is True.
192 199 """
193 200 if session == 0 or session==self.session_number: # Current session
194 201 return self._get_hist_session(start, stop, raw, output)
195 202 if session < 0:
196 203 session += self.session_number
197 204
198 205 # Assemble the SQL query:
199 206 sqlfrom = "history"
200 toget = "session, line, " +('source_raw' if raw else 'source')
207 toget = 'source_raw' if raw else 'source'
201 208 if output:
202 209 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
203 210 toget = "history.%s, output_history.output" % toget
204 211 if stop:
205 212 lineclause = "line >= ? AND line < ?"
206 213 params = (session, start, stop)
207 214 else:
208 215 lineclause = "line>=?"
209 216 params = (session, start)
210 217
211 cur = self.db.execute("SELECT %s FROM %s WHERE session==? AND %s"\
212 %(toget, sqlfrom, lineclause), params)
218 cur = self.db.execute("""SELECT session, line, %s FROM %s WHERE
219 session==? AND %s""" %(toget, sqlfrom, lineclause), params)
213 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 222 return cur
216 223
217 224 def get_hist_from_rangestr(self, rangestr, raw=True, output=False):
218 225 """Get lines of history from a string of ranges, as used by magic
219 226 commands %hist, %save, %macro, etc."""
220 227 for sess, s, e in extract_hist_ranges(rangestr):
221 228 for line in self.get_history(sess, s, e, raw=raw, output=output):
222 229 yield line
223 230
224 231 def store_inputs(self, line_num, source, source_raw=None):
225 232 """Store source and raw input in history and create input cache
226 233 variables _i*.
227 234
228 235 Parameters
229 236 ----------
230 237 line_num : int
231 238 The prompt number of this input.
232 239
233 240 source : str
234 241 Python input.
235 242
236 243 source_raw : str, optional
237 244 If given, this is the raw input without any IPython transformations
238 245 applied to it. If not given, ``source`` is used.
239 246 """
240 247 if source_raw is None:
241 248 source_raw = source
242 249
243 250 # do not store exit/quit commands
244 251 if source_raw.strip() in self._exit_commands:
245 252 return
246 253
247 254 self.input_hist_parsed.append(source.rstrip())
248 255 self.input_hist_raw.append(source_raw.rstrip())
249 256
250 257 self.db_input_cache.append((self.session_number, line_num,
251 258 source, source_raw))
252 259 # Trigger to flush cache and write to DB.
253 260 if len(self.db_input_cache) >= self.db_cache_size:
254 261 self.writeout_cache()
255 262
256 263 # update the auto _i variables
257 264 self._iii = self._ii
258 265 self._ii = self._i
259 266 self._i = self._i00
260 267 self._i00 = source_raw
261 268
262 269 # hackish access to user namespace to create _i1,_i2... dynamically
263 270 new_i = '_i%s' % line_num
264 271 to_main = {'_i': self._i,
265 272 '_ii': self._ii,
266 273 '_iii': self._iii,
267 274 new_i : self._i00 }
268 275 self.shell.user_ns.update(to_main)
269 276
270 277 def store_output(self, line_num, output):
271 278 if not self.db_log_output:
272 279 return
273 280 db_row = (self.session_number, line_num, output)
274 281 if self.db_cache_size > 1:
275 282 self.db_output_cache.append(db_row)
276 283 else:
277 284 with self.db:
278 285 self.db.execute("INSERT INTO output_history VALUES (?,?,?)", db_row)
279 286
280 287 def writeout_cache(self):
288 #print(self.db_input_cache)
281 289 with self.db:
282 290 self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)",
283 291 self.db_input_cache)
284 292 self.db.executemany("INSERT INTO output_history VALUES (?, ?, ?)",
285 293 self.db_output_cache)
286 294 self.db_input_cache = []
287 295 self.db_output_cache = []
288 296
289 297 def sync_inputs(self):
290 298 """Ensure raw and translated histories have same length."""
291 299 lr = len(self.input_hist_raw)
292 300 lp = len(self.input_hist_parsed)
293 301 if lp < lr:
294 302 self.input_hist_raw[:lr-lp] = []
295 303 elif lr < lp:
296 304 self.input_hist_parsed[:lp-lr] = []
297 305
298 def reset(self):
299 """Clear all histories managed by this object, and start a new
306 def reset(self, new_session=True):
307 """Clear the current session's history, and (optionally) start a new
300 308 session."""
301 309 self.input_hist_parsed[:] = [""]
302 310 self.input_hist_raw[:] = [""]
303 311 self.output_hist.clear()
304 312 # The directory history can't be completely empty
305 313 self.dir_hist[:] = [os.getcwd()]
306 314
307 self.writeout_cache()
308 self.init_db() # New session
315 if new_session:
316 self.writeout_cache()
317 self.init_db() # Get new session number
309 318
310 319 # To match, e.g. ~5/8-~2/3
311 320 range_re = re.compile(r"""
312 321 ((?P<startsess>~?\d+)/)?
313 322 (?P<start>\d+) # Only the start line num is compulsory
314 323 ((?P<sep>[\-:])
315 324 ((?P<endsess>~?\d+)/)?
316 325 (?P<end>\d+))?
317 326 """, re.VERBOSE)
318 327
319 328 def extract_hist_ranges(ranges_str):
320 329 """Turn a string of history ranges into 3-tuples of (session, start, stop).
321 330
322 331 Examples
323 332 --------
324 333 list(extract_input_ranges("~8/5-~7/4 2"))
325 334 [(-8, 5, None), (-7, 1, 4), (0, 2, 3)]
326 335 """
327 336 for range_str in ranges_str.split():
328 337 rmatch = range_re.match(range_str)
329 338 start = int(rmatch.group("start"))
330 339 end = rmatch.group("end")
331 340 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
332 341 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
333 342 end += 1
334 343 startsess = rmatch.group("startsess") or "0"
335 344 endsess = rmatch.group("endsess") or startsess
336 345 startsess = int(startsess.replace("~","-"))
337 346 endsess = int(endsess.replace("~","-"))
338 347 assert endsess >= startsess
339 348
340 349 if endsess == startsess:
341 350 yield (startsess, start, end)
342 351 continue
343 352 # Multiple sessions in one range:
344 353 yield (startsess, start, None)
345 354 for sess in range(startsess+1, endsess):
346 355 yield (sess, 1, None)
347 356 yield (endsess, 1, end)
348 357
349 358 def _format_lineno(session, line):
350 359 """Helper function to format line numbers properly."""
351 360 if session == 0:
352 361 return str(line)
353 362 return "%s#%s" % (session, line)
354 363
355 364 @testdec.skip_doctest
356 365 def magic_history(self, parameter_s = ''):
357 366 """Print input history (_i<n> variables), with most recent last.
358 367
359 368 %history -> print at most 40 inputs (some may be multi-line)\\
360 369 %history n -> print at most n inputs\\
361 370 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
362 371
363 372 By default, input history is printed without line numbers so it can be
364 373 directly pasted into an editor.
365 374
366 375 With -n, each input's number <n> is shown, and is accessible as the
367 376 automatically generated variable _i<n> as well as In[<n>]. Multi-line
368 377 statements are printed starting at a new line for easy copy/paste.
369 378
370 379 Options:
371 380
372 381 -n: print line numbers for each input.
373 382 This feature is only available if numbered prompts are in use.
374 383
375 384 -o: also print outputs for each input.
376 385
377 386 -p: print classic '>>>' python prompts before each input. This is useful
378 387 for making documentation, and in conjunction with -o, for producing
379 388 doctest-ready output.
380 389
381 390 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
382 391
383 392 -t: print the 'translated' history, as IPython understands it. IPython
384 393 filters your input and converts it all into valid Python source before
385 394 executing it (things like magics or aliases are turned into function
386 395 calls, for example). With this option, you'll see the native history
387 396 instead of the user-entered version: '%cd /' will be seen as
388 397 'get_ipython().magic("%cd /")' instead of '%cd /'.
389 398
390 399 -g: treat the arg as a pattern to grep for in (full) history.
391 400 This includes the saved history (almost all commands ever written).
392 401 Use '%hist -g' to show full saved history (may be very long).
393 402
394 403 -l: get the last n lines from all sessions. Specify n as a single arg, or
395 404 the default is the last 10 lines.
396 405
397 406 -f FILENAME: instead of printing the output to the screen, redirect it to
398 407 the given file. The file is always overwritten, though IPython asks for
399 408 confirmation first if it already exists.
400 409
401 410 Examples
402 411 --------
403 412 ::
404 413
405 414 In [6]: %hist -n 4 6
406 415 4:a = 12
407 416 5:print a**2
408 417
409 418 """
410 419
411 420 if not self.shell.displayhook.do_full_cache:
412 421 print('This feature is only available if numbered prompts are in use.')
413 422 return
414 423 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
415 424
416 425 # For brevity
417 426 history_manager = self.shell.history_manager
418 427
419 428 # Check if output to specific file was requested.
420 429 try:
421 430 outfname = opts['f']
422 431 except KeyError:
423 432 outfile = IPython.utils.io.Term.cout # default
424 433 # We don't want to close stdout at the end!
425 434 close_at_end = False
426 435 else:
427 436 if os.path.exists(outfname):
428 437 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
429 438 print('Aborting.')
430 439 return
431 440
432 441 outfile = open(outfname,'w')
433 442 close_at_end = True
434 443
435 444 print_nums = 'n' in opts
436 445 get_output = 'o' in opts
437 446 pyprompts = 'p' in opts
438 447 # Raw history is the default
439 448 raw = not('t' in opts)
440 449
441 450 default_length = 40
442 451 pattern = None
443 452
444 453 # Glob search:
445 454 if 'g' in opts:
446 455 pattern = "*" + args + "*" if args else "*"
447 456
448 457 # Display:
449 458 matches_current_session = []
450 459 for session, line, s in history_manager.get_hist_search(pattern, raw):
451 460 if session == history_manager.session_number:
452 461 matches_current_session.append(line, s)
453 462 continue
454 463 print("%d#%d: %s" %(session, line, s.expandtabs(4)), file=outfile)
455 464 if matches_current_session:
456 465 print("=== Current session: ===", file=outfile)
457 466 for line, s in matches_current_session:
458 467 print("%d: %s" %(line, s.expandtabs(4)), file=outfile)
459 468 return
460 469
461 470 if 'l' in opts: # Get 'tail'
462 471 try:
463 472 n = int(args)
464 473 except ValueError, IndexError:
465 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 476 else:
468 477 if args: # Get history by ranges
469 478 hist = history_manager.get_hist_from_rangestr(args, raw, get_output)
470 479 else: # Just get history for the current session
471 480 hist = history_manager.get_history(raw=raw, output=get_output)
472 481 # Pull hist into a list, so we can get the widest number in it.
473 482 hist = list(hist)
483 if not hist:
484 return
474 485
475 486 width = max(len(_format_lineno(s, l)) for s, l, _ in hist)
476 487
477 488 for session, lineno, inline in hist:
478 489 # Print user history with tabs expanded to 4 spaces. The GUI clients
479 490 # use hard tabs for easier usability in auto-indented code, but we want
480 491 # to produce PEP-8 compliant history for safe pasting into an editor.
481 492 if get_output:
482 493 inline, output = inline
483 494 inline = inline.expandtabs(4).rstrip()
484 495
485 496 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
486 497 continue
487 498
488 499 multiline = "\n" in inline
489 500 line_sep = '\n' if multiline else ''
490 501 if print_nums:
491 502 print('%s:%s' % (_format_lineno(session, lineno).ljust(width),
492 503 line_sep), file=outfile, end='')
493 504 if pyprompts:
494 505 print(">>> ", end="", file=outfile)
495 506 if multiline:
496 507 inline = "\n... ".join(inline.splitlines()) + "\n..."
497 508 print(inline, file=outfile)
498 509 if get_output and output:
499 print(repr(output), file=outfile)
510 print(output, file=outfile)
500 511
501 512 if close_at_end:
502 513 outfile.close()
503 514
504 515 # %hist is an alternative name
505 516 magic_hist = magic_history
506 517
507 518
508 519 def rep_f(self, arg):
509 520 r""" Repeat a command, or get command to input line for editing
510 521
511 522 - %rep (no arguments):
512 523
513 524 Place a string version of last computation result (stored in the special '_'
514 525 variable) to the next input prompt. Allows you to create elaborate command
515 526 lines without using copy-paste::
516 527
517 528 $ l = ["hei", "vaan"]
518 529 $ "".join(l)
519 530 ==> heivaan
520 531 $ %rep
521 532 $ heivaan_ <== cursor blinking
522 533
523 534 %rep 45
524 535
525 536 Place history line 45 to next input prompt. Use %hist to find out the
526 537 number.
527 538
528 539 %rep 1-4 6-7 3
529 540
530 541 Repeat the specified lines immediately. Input slice syntax is the same as
531 542 in %macro and %save.
532 543
533 544 %rep foo
534 545
535 546 Place the most recent line that has the substring "foo" to next input.
536 547 (e.g. 'svn ci -m foobar').
537 548 """
538 549
539 550 opts,args = self.parse_options(arg,'',mode='list')
540 551 if not args:
541 552 self.set_next_input(str(self.shell.user_ns["_"]))
542 553 return
543 554
544 555 if len(args) == 1 and not '-' in args[0]:
545 556 arg = args[0]
546 557 if len(arg) > 1 and arg.startswith('0'):
547 558 # get from shadow hist
548 559 num = int(arg[1:])
549 560 line = self.shell.shadowhist.get(num)
550 561 self.set_next_input(str(line))
551 562 return
552 563 try:
553 564 num = int(args[0])
554 565 self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip())
555 566 return
556 567 except ValueError:
557 568 pass
558 569
559 570 for h in reversed(self.shell.input_hist_raw):
560 571 if 'rep' in h:
561 572 continue
562 573 if fnmatch.fnmatch(h,'*' + arg + '*'):
563 574 self.set_next_input(str(h).rstrip())
564 575 return
565 576
566 577 try:
567 578 lines = self.extract_input_slices(args, True)
568 579 print("lines", lines)
569 580 self.run_cell(lines)
570 581 except ValueError:
571 582 print("Not found in recent history:", args)
572 583
573 584
574 585 def init_ipython(ip):
575 586 ip.define_magic("rep",rep_f)
576 587 ip.define_magic("hist",magic_hist)
577 588 ip.define_magic("history",magic_history)
578 589
579 590 # XXX - ipy_completers are in quarantine, need to be updated to new apis
580 591 #import ipy_completers
581 592 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,2549 +1,2547 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import __future__
22 22 import abc
23 23 import atexit
24 24 import codeop
25 25 import os
26 26 import re
27 27 import sys
28 28 import tempfile
29 29 import types
30 30 from contextlib import nested
31 31
32 32 from IPython.config.configurable import Configurable
33 33 from IPython.core import debugger, oinspect
34 34 from IPython.core import history as ipcorehist
35 35 from IPython.core import page
36 36 from IPython.core import prefilter
37 37 from IPython.core import shadowns
38 38 from IPython.core import ultratb
39 39 from IPython.core.alias import AliasManager
40 40 from IPython.core.builtin_trap import BuiltinTrap
41 41 from IPython.core.compilerop import CachingCompiler
42 42 from IPython.core.display_trap import DisplayTrap
43 43 from IPython.core.displayhook import DisplayHook
44 44 from IPython.core.displaypub import DisplayPublisher
45 45 from IPython.core.error import TryNext, UsageError
46 46 from IPython.core.extensions import ExtensionManager
47 47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 48 from IPython.core.formatters import DisplayFormatter
49 49 from IPython.core.history import HistoryManager
50 50 from IPython.core.inputsplitter import IPythonInputSplitter
51 51 from IPython.core.logger import Logger
52 52 from IPython.core.magic import Magic
53 53 from IPython.core.payload import PayloadManager
54 54 from IPython.core.plugin import PluginManager
55 55 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
56 56 from IPython.external.Itpl import ItplNS
57 57 from IPython.utils import PyColorize
58 58 from IPython.utils import io
59 59 from IPython.utils.doctestreload import doctest_reload
60 60 from IPython.utils.io import ask_yes_no, rprint
61 61 from IPython.utils.ipstruct import Struct
62 62 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
63 63 from IPython.utils.pickleshare import PickleShareDB
64 64 from IPython.utils.process import system, getoutput
65 65 from IPython.utils.strdispatch import StrDispatch
66 66 from IPython.utils.syspathcontext import prepended_to_syspath
67 67 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
68 68 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
69 69 List, Unicode, Instance, Type)
70 70 from IPython.utils.warn import warn, error, fatal
71 71 import IPython.core.hooks
72 72
73 73 #-----------------------------------------------------------------------------
74 74 # Globals
75 75 #-----------------------------------------------------------------------------
76 76
77 77 # compiled regexps for autoindent management
78 78 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
79 79
80 80 #-----------------------------------------------------------------------------
81 81 # Utilities
82 82 #-----------------------------------------------------------------------------
83 83
84 84 # store the builtin raw_input globally, and use this always, in case user code
85 85 # overwrites it (like wx.py.PyShell does)
86 86 raw_input_original = raw_input
87 87
88 88 def softspace(file, newvalue):
89 89 """Copied from code.py, to remove the dependency"""
90 90
91 91 oldvalue = 0
92 92 try:
93 93 oldvalue = file.softspace
94 94 except AttributeError:
95 95 pass
96 96 try:
97 97 file.softspace = newvalue
98 98 except (AttributeError, TypeError):
99 99 # "attribute-less object" or "read-only attributes"
100 100 pass
101 101 return oldvalue
102 102
103 103
104 104 def no_op(*a, **kw): pass
105 105
106 106 class SpaceInInput(Exception): pass
107 107
108 108 class Bunch: pass
109 109
110 110
111 111 def get_default_colors():
112 112 if sys.platform=='darwin':
113 113 return "LightBG"
114 114 elif os.name=='nt':
115 115 return 'Linux'
116 116 else:
117 117 return 'Linux'
118 118
119 119
120 120 class SeparateStr(Str):
121 121 """A Str subclass to validate separate_in, separate_out, etc.
122 122
123 123 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
124 124 """
125 125
126 126 def validate(self, obj, value):
127 127 if value == '0': value = ''
128 128 value = value.replace('\\n','\n')
129 129 return super(SeparateStr, self).validate(obj, value)
130 130
131 131 class MultipleInstanceError(Exception):
132 132 pass
133 133
134 134
135 135 #-----------------------------------------------------------------------------
136 136 # Main IPython class
137 137 #-----------------------------------------------------------------------------
138 138
139 139 class InteractiveShell(Configurable, Magic):
140 140 """An enhanced, interactive shell for Python."""
141 141
142 142 _instance = None
143 143 autocall = Enum((0,1,2), default_value=1, config=True)
144 144 # TODO: remove all autoindent logic and put into frontends.
145 145 # We can't do this yet because even runlines uses the autoindent.
146 146 autoindent = CBool(True, config=True)
147 147 automagic = CBool(True, config=True)
148 148 cache_size = Int(1000, config=True)
149 149 color_info = CBool(True, config=True)
150 150 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
151 151 default_value=get_default_colors(), config=True)
152 152 debug = CBool(False, config=True)
153 153 deep_reload = CBool(False, config=True)
154 154 display_formatter = Instance(DisplayFormatter)
155 155 displayhook_class = Type(DisplayHook)
156 156 display_pub_class = Type(DisplayPublisher)
157 157
158 158 exit_now = CBool(False)
159 159 # Monotonically increasing execution counter
160 160 execution_count = Int(1)
161 161 filename = Str("<ipython console>")
162 162 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
163 163
164 164 # Input splitter, to split entire cells of input into either individual
165 165 # interactive statements or whole blocks.
166 166 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
167 167 (), {})
168 168 logstart = CBool(False, config=True)
169 169 logfile = Str('', config=True)
170 170 logappend = Str('', config=True)
171 171 object_info_string_level = Enum((0,1,2), default_value=0,
172 172 config=True)
173 173 pdb = CBool(False, config=True)
174 174
175 175 profile = Str('', config=True)
176 176 prompt_in1 = Str('In [\\#]: ', config=True)
177 177 prompt_in2 = Str(' .\\D.: ', config=True)
178 178 prompt_out = Str('Out[\\#]: ', config=True)
179 179 prompts_pad_left = CBool(True, config=True)
180 180 quiet = CBool(False, config=True)
181 181
182 182 history_length = Int(10000, config=True)
183 183
184 184 # The readline stuff will eventually be moved to the terminal subclass
185 185 # but for now, we can't do that as readline is welded in everywhere.
186 186 readline_use = CBool(True, config=True)
187 187 readline_merge_completions = CBool(True, config=True)
188 188 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
189 189 readline_remove_delims = Str('-/~', config=True)
190 190 readline_parse_and_bind = List([
191 191 'tab: complete',
192 192 '"\C-l": clear-screen',
193 193 'set show-all-if-ambiguous on',
194 194 '"\C-o": tab-insert',
195 195 '"\M-i": " "',
196 196 '"\M-o": "\d\d\d\d"',
197 197 '"\M-I": "\d\d\d\d"',
198 198 '"\C-r": reverse-search-history',
199 199 '"\C-s": forward-search-history',
200 200 '"\C-p": history-search-backward',
201 201 '"\C-n": history-search-forward',
202 202 '"\e[A": history-search-backward',
203 203 '"\e[B": history-search-forward',
204 204 '"\C-k": kill-line',
205 205 '"\C-u": unix-line-discard',
206 206 ], allow_none=False, config=True)
207 207
208 208 # TODO: this part of prompt management should be moved to the frontends.
209 209 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
210 210 separate_in = SeparateStr('\n', config=True)
211 211 separate_out = SeparateStr('', config=True)
212 212 separate_out2 = SeparateStr('', config=True)
213 213 wildcards_case_sensitive = CBool(True, config=True)
214 214 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
215 215 default_value='Context', config=True)
216 216
217 217 # Subcomponents of InteractiveShell
218 218 alias_manager = Instance('IPython.core.alias.AliasManager')
219 219 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
220 220 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
221 221 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
222 222 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
223 223 plugin_manager = Instance('IPython.core.plugin.PluginManager')
224 224 payload_manager = Instance('IPython.core.payload.PayloadManager')
225 225 history_manager = Instance('IPython.core.history.HistoryManager')
226 226
227 227 # Private interface
228 228 _post_execute = set()
229 229
230 230 def __init__(self, config=None, ipython_dir=None,
231 231 user_ns=None, user_global_ns=None,
232 232 custom_exceptions=((), None)):
233 233
234 234 # This is where traits with a config_key argument are updated
235 235 # from the values on config.
236 236 super(InteractiveShell, self).__init__(config=config)
237 237
238 238 # These are relatively independent and stateless
239 239 self.init_ipython_dir(ipython_dir)
240 240 self.init_instance_attrs()
241 241 self.init_environment()
242 242
243 243 # Create namespaces (user_ns, user_global_ns, etc.)
244 244 self.init_create_namespaces(user_ns, user_global_ns)
245 245 # This has to be done after init_create_namespaces because it uses
246 246 # something in self.user_ns, but before init_sys_modules, which
247 247 # is the first thing to modify sys.
248 248 # TODO: When we override sys.stdout and sys.stderr before this class
249 249 # is created, we are saving the overridden ones here. Not sure if this
250 250 # is what we want to do.
251 251 self.save_sys_module_state()
252 252 self.init_sys_modules()
253 253
254 254 # While we're trying to have each part of the code directly access what
255 255 # it needs without keeping redundant references to objects, we have too
256 256 # much legacy code that expects ip.db to exist.
257 257 self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db'))
258 258
259 259 self.init_history()
260 260 self.init_encoding()
261 261 self.init_prefilter()
262 262
263 263 Magic.__init__(self, self)
264 264
265 265 self.init_syntax_highlighting()
266 266 self.init_hooks()
267 267 self.init_pushd_popd_magic()
268 268 # self.init_traceback_handlers use to be here, but we moved it below
269 269 # because it and init_io have to come after init_readline.
270 270 self.init_user_ns()
271 271 self.init_logger()
272 272 self.init_alias()
273 273 self.init_builtins()
274 274
275 275 # pre_config_initialization
276 276
277 277 # The next section should contain everything that was in ipmaker.
278 278 self.init_logstart()
279 279
280 280 # The following was in post_config_initialization
281 281 self.init_inspector()
282 282 # init_readline() must come before init_io(), because init_io uses
283 283 # readline related things.
284 284 self.init_readline()
285 285 # init_completer must come after init_readline, because it needs to
286 286 # know whether readline is present or not system-wide to configure the
287 287 # completers, since the completion machinery can now operate
288 288 # independently of readline (e.g. over the network)
289 289 self.init_completer()
290 290 # TODO: init_io() needs to happen before init_traceback handlers
291 291 # because the traceback handlers hardcode the stdout/stderr streams.
292 292 # This logic in in debugger.Pdb and should eventually be changed.
293 293 self.init_io()
294 294 self.init_traceback_handlers(custom_exceptions)
295 295 self.init_prompts()
296 296 self.init_display_formatter()
297 297 self.init_display_pub()
298 298 self.init_displayhook()
299 299 self.init_reload_doctest()
300 300 self.init_magics()
301 301 self.init_pdb()
302 302 self.init_extension_manager()
303 303 self.init_plugin_manager()
304 304 self.init_payload()
305 305 self.hooks.late_startup_hook()
306 306 atexit.register(self.atexit_operations)
307 307
308 308 @classmethod
309 309 def instance(cls, *args, **kwargs):
310 310 """Returns a global InteractiveShell instance."""
311 311 if cls._instance is None:
312 312 inst = cls(*args, **kwargs)
313 313 # Now make sure that the instance will also be returned by
314 314 # the subclasses instance attribute.
315 315 for subclass in cls.mro():
316 316 if issubclass(cls, subclass) and \
317 317 issubclass(subclass, InteractiveShell):
318 318 subclass._instance = inst
319 319 else:
320 320 break
321 321 if isinstance(cls._instance, cls):
322 322 return cls._instance
323 323 else:
324 324 raise MultipleInstanceError(
325 325 'Multiple incompatible subclass instances of '
326 326 'InteractiveShell are being created.'
327 327 )
328 328
329 329 @classmethod
330 330 def initialized(cls):
331 331 return hasattr(cls, "_instance")
332 332
333 333 def get_ipython(self):
334 334 """Return the currently running IPython instance."""
335 335 return self
336 336
337 337 #-------------------------------------------------------------------------
338 338 # Trait changed handlers
339 339 #-------------------------------------------------------------------------
340 340
341 341 def _ipython_dir_changed(self, name, new):
342 342 if not os.path.isdir(new):
343 343 os.makedirs(new, mode = 0777)
344 344
345 345 def set_autoindent(self,value=None):
346 346 """Set the autoindent flag, checking for readline support.
347 347
348 348 If called with no arguments, it acts as a toggle."""
349 349
350 350 if not self.has_readline:
351 351 if os.name == 'posix':
352 352 warn("The auto-indent feature requires the readline library")
353 353 self.autoindent = 0
354 354 return
355 355 if value is None:
356 356 self.autoindent = not self.autoindent
357 357 else:
358 358 self.autoindent = value
359 359
360 360 #-------------------------------------------------------------------------
361 361 # init_* methods called by __init__
362 362 #-------------------------------------------------------------------------
363 363
364 364 def init_ipython_dir(self, ipython_dir):
365 365 if ipython_dir is not None:
366 366 self.ipython_dir = ipython_dir
367 367 self.config.Global.ipython_dir = self.ipython_dir
368 368 return
369 369
370 370 if hasattr(self.config.Global, 'ipython_dir'):
371 371 self.ipython_dir = self.config.Global.ipython_dir
372 372 else:
373 373 self.ipython_dir = get_ipython_dir()
374 374
375 375 # All children can just read this
376 376 self.config.Global.ipython_dir = self.ipython_dir
377 377
378 378 def init_instance_attrs(self):
379 379 self.more = False
380 380
381 381 # command compiler
382 382 self.compile = CachingCompiler()
383 383
384 384 # User input buffers
385 385 # NOTE: these variables are slated for full removal, once we are 100%
386 386 # sure that the new execution logic is solid. We will delte runlines,
387 387 # push_line and these buffers, as all input will be managed by the
388 388 # frontends via an inputsplitter instance.
389 389 self.buffer = []
390 390 self.buffer_raw = []
391 391
392 392 # Make an empty namespace, which extension writers can rely on both
393 393 # existing and NEVER being used by ipython itself. This gives them a
394 394 # convenient location for storing additional information and state
395 395 # their extensions may require, without fear of collisions with other
396 396 # ipython names that may develop later.
397 397 self.meta = Struct()
398 398
399 399 # Object variable to store code object waiting execution. This is
400 400 # used mainly by the multithreaded shells, but it can come in handy in
401 401 # other situations. No need to use a Queue here, since it's a single
402 402 # item which gets cleared once run.
403 403 self.code_to_run = None
404 404
405 405 # Temporary files used for various purposes. Deleted at exit.
406 406 self.tempfiles = []
407 407
408 408 # Keep track of readline usage (later set by init_readline)
409 409 self.has_readline = False
410 410
411 411 # keep track of where we started running (mainly for crash post-mortem)
412 412 # This is not being used anywhere currently.
413 413 self.starting_dir = os.getcwd()
414 414
415 415 # Indentation management
416 416 self.indent_current_nsp = 0
417 417
418 418 def init_environment(self):
419 419 """Any changes we need to make to the user's environment."""
420 420 pass
421 421
422 422 def init_encoding(self):
423 423 # Get system encoding at startup time. Certain terminals (like Emacs
424 424 # under Win32 have it set to None, and we need to have a known valid
425 425 # encoding to use in the raw_input() method
426 426 try:
427 427 self.stdin_encoding = sys.stdin.encoding or 'ascii'
428 428 except AttributeError:
429 429 self.stdin_encoding = 'ascii'
430 430
431 431 def init_syntax_highlighting(self):
432 432 # Python source parser/formatter for syntax highlighting
433 433 pyformat = PyColorize.Parser().format
434 434 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
435 435
436 436 def init_pushd_popd_magic(self):
437 437 # for pushd/popd management
438 438 try:
439 439 self.home_dir = get_home_dir()
440 440 except HomeDirError, msg:
441 441 fatal(msg)
442 442
443 443 self.dir_stack = []
444 444
445 445 def init_logger(self):
446 446 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
447 447 logmode='rotate')
448 448
449 449 def init_logstart(self):
450 450 """Initialize logging in case it was requested at the command line.
451 451 """
452 452 if self.logappend:
453 453 self.magic_logstart(self.logappend + ' append')
454 454 elif self.logfile:
455 455 self.magic_logstart(self.logfile)
456 456 elif self.logstart:
457 457 self.magic_logstart()
458 458
459 459 def init_builtins(self):
460 460 self.builtin_trap = BuiltinTrap(shell=self)
461 461
462 462 def init_inspector(self):
463 463 # Object inspector
464 464 self.inspector = oinspect.Inspector(oinspect.InspectColors,
465 465 PyColorize.ANSICodeColors,
466 466 'NoColor',
467 467 self.object_info_string_level)
468 468
469 469 def init_io(self):
470 470 # This will just use sys.stdout and sys.stderr. If you want to
471 471 # override sys.stdout and sys.stderr themselves, you need to do that
472 472 # *before* instantiating this class, because Term holds onto
473 473 # references to the underlying streams.
474 474 if sys.platform == 'win32' and self.has_readline:
475 475 Term = io.IOTerm(cout=self.readline._outputfile,
476 476 cerr=self.readline._outputfile)
477 477 else:
478 478 Term = io.IOTerm()
479 479 io.Term = Term
480 480
481 481 def init_prompts(self):
482 482 # TODO: This is a pass for now because the prompts are managed inside
483 483 # the DisplayHook. Once there is a separate prompt manager, this
484 484 # will initialize that object and all prompt related information.
485 485 pass
486 486
487 487 def init_display_formatter(self):
488 488 self.display_formatter = DisplayFormatter(config=self.config)
489 489
490 490 def init_display_pub(self):
491 491 self.display_pub = self.display_pub_class(config=self.config)
492 492
493 493 def init_displayhook(self):
494 494 # Initialize displayhook, set in/out prompts and printing system
495 495 self.displayhook = self.displayhook_class(
496 496 config=self.config,
497 497 shell=self,
498 498 cache_size=self.cache_size,
499 499 input_sep = self.separate_in,
500 500 output_sep = self.separate_out,
501 501 output_sep2 = self.separate_out2,
502 502 ps1 = self.prompt_in1,
503 503 ps2 = self.prompt_in2,
504 504 ps_out = self.prompt_out,
505 505 pad_left = self.prompts_pad_left
506 506 )
507 507 # This is a context manager that installs/revmoes the displayhook at
508 508 # the appropriate time.
509 509 self.display_trap = DisplayTrap(hook=self.displayhook)
510 510
511 511 def init_reload_doctest(self):
512 512 # Do a proper resetting of doctest, including the necessary displayhook
513 513 # monkeypatching
514 514 try:
515 515 doctest_reload()
516 516 except ImportError:
517 517 warn("doctest module does not exist.")
518 518
519 519 #-------------------------------------------------------------------------
520 520 # Things related to injections into the sys module
521 521 #-------------------------------------------------------------------------
522 522
523 523 def save_sys_module_state(self):
524 524 """Save the state of hooks in the sys module.
525 525
526 526 This has to be called after self.user_ns is created.
527 527 """
528 528 self._orig_sys_module_state = {}
529 529 self._orig_sys_module_state['stdin'] = sys.stdin
530 530 self._orig_sys_module_state['stdout'] = sys.stdout
531 531 self._orig_sys_module_state['stderr'] = sys.stderr
532 532 self._orig_sys_module_state['excepthook'] = sys.excepthook
533 533 try:
534 534 self._orig_sys_modules_main_name = self.user_ns['__name__']
535 535 except KeyError:
536 536 pass
537 537
538 538 def restore_sys_module_state(self):
539 539 """Restore the state of the sys module."""
540 540 try:
541 541 for k, v in self._orig_sys_module_state.iteritems():
542 542 setattr(sys, k, v)
543 543 except AttributeError:
544 544 pass
545 545 # Reset what what done in self.init_sys_modules
546 546 try:
547 547 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
548 548 except (AttributeError, KeyError):
549 549 pass
550 550
551 551 #-------------------------------------------------------------------------
552 552 # Things related to hooks
553 553 #-------------------------------------------------------------------------
554 554
555 555 def init_hooks(self):
556 556 # hooks holds pointers used for user-side customizations
557 557 self.hooks = Struct()
558 558
559 559 self.strdispatchers = {}
560 560
561 561 # Set all default hooks, defined in the IPython.hooks module.
562 562 hooks = IPython.core.hooks
563 563 for hook_name in hooks.__all__:
564 564 # default hooks have priority 100, i.e. low; user hooks should have
565 565 # 0-100 priority
566 566 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
567 567
568 568 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
569 569 """set_hook(name,hook) -> sets an internal IPython hook.
570 570
571 571 IPython exposes some of its internal API as user-modifiable hooks. By
572 572 adding your function to one of these hooks, you can modify IPython's
573 573 behavior to call at runtime your own routines."""
574 574
575 575 # At some point in the future, this should validate the hook before it
576 576 # accepts it. Probably at least check that the hook takes the number
577 577 # of args it's supposed to.
578 578
579 579 f = types.MethodType(hook,self)
580 580
581 581 # check if the hook is for strdispatcher first
582 582 if str_key is not None:
583 583 sdp = self.strdispatchers.get(name, StrDispatch())
584 584 sdp.add_s(str_key, f, priority )
585 585 self.strdispatchers[name] = sdp
586 586 return
587 587 if re_key is not None:
588 588 sdp = self.strdispatchers.get(name, StrDispatch())
589 589 sdp.add_re(re.compile(re_key), f, priority )
590 590 self.strdispatchers[name] = sdp
591 591 return
592 592
593 593 dp = getattr(self.hooks, name, None)
594 594 if name not in IPython.core.hooks.__all__:
595 595 print "Warning! Hook '%s' is not one of %s" % \
596 596 (name, IPython.core.hooks.__all__ )
597 597 if not dp:
598 598 dp = IPython.core.hooks.CommandChainDispatcher()
599 599
600 600 try:
601 601 dp.add(f,priority)
602 602 except AttributeError:
603 603 # it was not commandchain, plain old func - replace
604 604 dp = f
605 605
606 606 setattr(self.hooks,name, dp)
607 607
608 608 def register_post_execute(self, func):
609 609 """Register a function for calling after code execution.
610 610 """
611 611 if not callable(func):
612 612 raise ValueError('argument %s must be callable' % func)
613 613 self._post_execute.add(func)
614 614
615 615 #-------------------------------------------------------------------------
616 616 # Things related to the "main" module
617 617 #-------------------------------------------------------------------------
618 618
619 619 def new_main_mod(self,ns=None):
620 620 """Return a new 'main' module object for user code execution.
621 621 """
622 622 main_mod = self._user_main_module
623 623 init_fakemod_dict(main_mod,ns)
624 624 return main_mod
625 625
626 626 def cache_main_mod(self,ns,fname):
627 627 """Cache a main module's namespace.
628 628
629 629 When scripts are executed via %run, we must keep a reference to the
630 630 namespace of their __main__ module (a FakeModule instance) around so
631 631 that Python doesn't clear it, rendering objects defined therein
632 632 useless.
633 633
634 634 This method keeps said reference in a private dict, keyed by the
635 635 absolute path of the module object (which corresponds to the script
636 636 path). This way, for multiple executions of the same script we only
637 637 keep one copy of the namespace (the last one), thus preventing memory
638 638 leaks from old references while allowing the objects from the last
639 639 execution to be accessible.
640 640
641 641 Note: we can not allow the actual FakeModule instances to be deleted,
642 642 because of how Python tears down modules (it hard-sets all their
643 643 references to None without regard for reference counts). This method
644 644 must therefore make a *copy* of the given namespace, to allow the
645 645 original module's __dict__ to be cleared and reused.
646 646
647 647
648 648 Parameters
649 649 ----------
650 650 ns : a namespace (a dict, typically)
651 651
652 652 fname : str
653 653 Filename associated with the namespace.
654 654
655 655 Examples
656 656 --------
657 657
658 658 In [10]: import IPython
659 659
660 660 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
661 661
662 662 In [12]: IPython.__file__ in _ip._main_ns_cache
663 663 Out[12]: True
664 664 """
665 665 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
666 666
667 667 def clear_main_mod_cache(self):
668 668 """Clear the cache of main modules.
669 669
670 670 Mainly for use by utilities like %reset.
671 671
672 672 Examples
673 673 --------
674 674
675 675 In [15]: import IPython
676 676
677 677 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
678 678
679 679 In [17]: len(_ip._main_ns_cache) > 0
680 680 Out[17]: True
681 681
682 682 In [18]: _ip.clear_main_mod_cache()
683 683
684 684 In [19]: len(_ip._main_ns_cache) == 0
685 685 Out[19]: True
686 686 """
687 687 self._main_ns_cache.clear()
688 688
689 689 #-------------------------------------------------------------------------
690 690 # Things related to debugging
691 691 #-------------------------------------------------------------------------
692 692
693 693 def init_pdb(self):
694 694 # Set calling of pdb on exceptions
695 695 # self.call_pdb is a property
696 696 self.call_pdb = self.pdb
697 697
698 698 def _get_call_pdb(self):
699 699 return self._call_pdb
700 700
701 701 def _set_call_pdb(self,val):
702 702
703 703 if val not in (0,1,False,True):
704 704 raise ValueError,'new call_pdb value must be boolean'
705 705
706 706 # store value in instance
707 707 self._call_pdb = val
708 708
709 709 # notify the actual exception handlers
710 710 self.InteractiveTB.call_pdb = val
711 711
712 712 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
713 713 'Control auto-activation of pdb at exceptions')
714 714
715 715 def debugger(self,force=False):
716 716 """Call the pydb/pdb debugger.
717 717
718 718 Keywords:
719 719
720 720 - force(False): by default, this routine checks the instance call_pdb
721 721 flag and does not actually invoke the debugger if the flag is false.
722 722 The 'force' option forces the debugger to activate even if the flag
723 723 is false.
724 724 """
725 725
726 726 if not (force or self.call_pdb):
727 727 return
728 728
729 729 if not hasattr(sys,'last_traceback'):
730 730 error('No traceback has been produced, nothing to debug.')
731 731 return
732 732
733 733 # use pydb if available
734 734 if debugger.has_pydb:
735 735 from pydb import pm
736 736 else:
737 737 # fallback to our internal debugger
738 738 pm = lambda : self.InteractiveTB.debugger(force=True)
739 739 self.history_saving_wrapper(pm)()
740 740
741 741 #-------------------------------------------------------------------------
742 742 # Things related to IPython's various namespaces
743 743 #-------------------------------------------------------------------------
744 744
745 745 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
746 746 # Create the namespace where the user will operate. user_ns is
747 747 # normally the only one used, and it is passed to the exec calls as
748 748 # the locals argument. But we do carry a user_global_ns namespace
749 749 # given as the exec 'globals' argument, This is useful in embedding
750 750 # situations where the ipython shell opens in a context where the
751 751 # distinction between locals and globals is meaningful. For
752 752 # non-embedded contexts, it is just the same object as the user_ns dict.
753 753
754 754 # FIXME. For some strange reason, __builtins__ is showing up at user
755 755 # level as a dict instead of a module. This is a manual fix, but I
756 756 # should really track down where the problem is coming from. Alex
757 757 # Schmolck reported this problem first.
758 758
759 759 # A useful post by Alex Martelli on this topic:
760 760 # Re: inconsistent value from __builtins__
761 761 # Von: Alex Martelli <aleaxit@yahoo.com>
762 762 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
763 763 # Gruppen: comp.lang.python
764 764
765 765 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
766 766 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
767 767 # > <type 'dict'>
768 768 # > >>> print type(__builtins__)
769 769 # > <type 'module'>
770 770 # > Is this difference in return value intentional?
771 771
772 772 # Well, it's documented that '__builtins__' can be either a dictionary
773 773 # or a module, and it's been that way for a long time. Whether it's
774 774 # intentional (or sensible), I don't know. In any case, the idea is
775 775 # that if you need to access the built-in namespace directly, you
776 776 # should start with "import __builtin__" (note, no 's') which will
777 777 # definitely give you a module. Yeah, it's somewhat confusing:-(.
778 778
779 779 # These routines return properly built dicts as needed by the rest of
780 780 # the code, and can also be used by extension writers to generate
781 781 # properly initialized namespaces.
782 782 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
783 783 user_global_ns)
784 784
785 785 # Assign namespaces
786 786 # This is the namespace where all normal user variables live
787 787 self.user_ns = user_ns
788 788 self.user_global_ns = user_global_ns
789 789
790 790 # An auxiliary namespace that checks what parts of the user_ns were
791 791 # loaded at startup, so we can list later only variables defined in
792 792 # actual interactive use. Since it is always a subset of user_ns, it
793 793 # doesn't need to be separately tracked in the ns_table.
794 794 self.user_ns_hidden = {}
795 795
796 796 # A namespace to keep track of internal data structures to prevent
797 797 # them from cluttering user-visible stuff. Will be updated later
798 798 self.internal_ns = {}
799 799
800 800 # Now that FakeModule produces a real module, we've run into a nasty
801 801 # problem: after script execution (via %run), the module where the user
802 802 # code ran is deleted. Now that this object is a true module (needed
803 803 # so docetst and other tools work correctly), the Python module
804 804 # teardown mechanism runs over it, and sets to None every variable
805 805 # present in that module. Top-level references to objects from the
806 806 # script survive, because the user_ns is updated with them. However,
807 807 # calling functions defined in the script that use other things from
808 808 # the script will fail, because the function's closure had references
809 809 # to the original objects, which are now all None. So we must protect
810 810 # these modules from deletion by keeping a cache.
811 811 #
812 812 # To avoid keeping stale modules around (we only need the one from the
813 813 # last run), we use a dict keyed with the full path to the script, so
814 814 # only the last version of the module is held in the cache. Note,
815 815 # however, that we must cache the module *namespace contents* (their
816 816 # __dict__). Because if we try to cache the actual modules, old ones
817 817 # (uncached) could be destroyed while still holding references (such as
818 818 # those held by GUI objects that tend to be long-lived)>
819 819 #
820 820 # The %reset command will flush this cache. See the cache_main_mod()
821 821 # and clear_main_mod_cache() methods for details on use.
822 822
823 823 # This is the cache used for 'main' namespaces
824 824 self._main_ns_cache = {}
825 825 # And this is the single instance of FakeModule whose __dict__ we keep
826 826 # copying and clearing for reuse on each %run
827 827 self._user_main_module = FakeModule()
828 828
829 829 # A table holding all the namespaces IPython deals with, so that
830 830 # introspection facilities can search easily.
831 831 self.ns_table = {'user':user_ns,
832 832 'user_global':user_global_ns,
833 833 'internal':self.internal_ns,
834 834 'builtin':__builtin__.__dict__
835 835 }
836 836
837 837 # Similarly, track all namespaces where references can be held and that
838 838 # we can safely clear (so it can NOT include builtin). This one can be
839 839 # a simple list. Note that the main execution namespaces, user_ns and
840 840 # user_global_ns, can NOT be listed here, as clearing them blindly
841 841 # causes errors in object __del__ methods. Instead, the reset() method
842 842 # clears them manually and carefully.
843 843 self.ns_refs_table = [ self.user_ns_hidden,
844 844 self.internal_ns, self._main_ns_cache ]
845 845
846 846 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
847 847 """Return a valid local and global user interactive namespaces.
848 848
849 849 This builds a dict with the minimal information needed to operate as a
850 850 valid IPython user namespace, which you can pass to the various
851 851 embedding classes in ipython. The default implementation returns the
852 852 same dict for both the locals and the globals to allow functions to
853 853 refer to variables in the namespace. Customized implementations can
854 854 return different dicts. The locals dictionary can actually be anything
855 855 following the basic mapping protocol of a dict, but the globals dict
856 856 must be a true dict, not even a subclass. It is recommended that any
857 857 custom object for the locals namespace synchronize with the globals
858 858 dict somehow.
859 859
860 860 Raises TypeError if the provided globals namespace is not a true dict.
861 861
862 862 Parameters
863 863 ----------
864 864 user_ns : dict-like, optional
865 865 The current user namespace. The items in this namespace should
866 866 be included in the output. If None, an appropriate blank
867 867 namespace should be created.
868 868 user_global_ns : dict, optional
869 869 The current user global namespace. The items in this namespace
870 870 should be included in the output. If None, an appropriate
871 871 blank namespace should be created.
872 872
873 873 Returns
874 874 -------
875 875 A pair of dictionary-like object to be used as the local namespace
876 876 of the interpreter and a dict to be used as the global namespace.
877 877 """
878 878
879 879
880 880 # We must ensure that __builtin__ (without the final 's') is always
881 881 # available and pointing to the __builtin__ *module*. For more details:
882 882 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
883 883
884 884 if user_ns is None:
885 885 # Set __name__ to __main__ to better match the behavior of the
886 886 # normal interpreter.
887 887 user_ns = {'__name__' :'__main__',
888 888 '__builtin__' : __builtin__,
889 889 '__builtins__' : __builtin__,
890 890 }
891 891 else:
892 892 user_ns.setdefault('__name__','__main__')
893 893 user_ns.setdefault('__builtin__',__builtin__)
894 894 user_ns.setdefault('__builtins__',__builtin__)
895 895
896 896 if user_global_ns is None:
897 897 user_global_ns = user_ns
898 898 if type(user_global_ns) is not dict:
899 899 raise TypeError("user_global_ns must be a true dict; got %r"
900 900 % type(user_global_ns))
901 901
902 902 return user_ns, user_global_ns
903 903
904 904 def init_sys_modules(self):
905 905 # We need to insert into sys.modules something that looks like a
906 906 # module but which accesses the IPython namespace, for shelve and
907 907 # pickle to work interactively. Normally they rely on getting
908 908 # everything out of __main__, but for embedding purposes each IPython
909 909 # instance has its own private namespace, so we can't go shoving
910 910 # everything into __main__.
911 911
912 912 # note, however, that we should only do this for non-embedded
913 913 # ipythons, which really mimic the __main__.__dict__ with their own
914 914 # namespace. Embedded instances, on the other hand, should not do
915 915 # this because they need to manage the user local/global namespaces
916 916 # only, but they live within a 'normal' __main__ (meaning, they
917 917 # shouldn't overtake the execution environment of the script they're
918 918 # embedded in).
919 919
920 920 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
921 921
922 922 try:
923 923 main_name = self.user_ns['__name__']
924 924 except KeyError:
925 925 raise KeyError('user_ns dictionary MUST have a "__name__" key')
926 926 else:
927 927 sys.modules[main_name] = FakeModule(self.user_ns)
928 928
929 929 def init_user_ns(self):
930 930 """Initialize all user-visible namespaces to their minimum defaults.
931 931
932 932 Certain history lists are also initialized here, as they effectively
933 933 act as user namespaces.
934 934
935 935 Notes
936 936 -----
937 937 All data structures here are only filled in, they are NOT reset by this
938 938 method. If they were not empty before, data will simply be added to
939 939 therm.
940 940 """
941 941 # This function works in two parts: first we put a few things in
942 942 # user_ns, and we sync that contents into user_ns_hidden so that these
943 943 # initial variables aren't shown by %who. After the sync, we add the
944 944 # rest of what we *do* want the user to see with %who even on a new
945 945 # session (probably nothing, so theye really only see their own stuff)
946 946
947 947 # The user dict must *always* have a __builtin__ reference to the
948 948 # Python standard __builtin__ namespace, which must be imported.
949 949 # This is so that certain operations in prompt evaluation can be
950 950 # reliably executed with builtins. Note that we can NOT use
951 951 # __builtins__ (note the 's'), because that can either be a dict or a
952 952 # module, and can even mutate at runtime, depending on the context
953 953 # (Python makes no guarantees on it). In contrast, __builtin__ is
954 954 # always a module object, though it must be explicitly imported.
955 955
956 956 # For more details:
957 957 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
958 958 ns = dict(__builtin__ = __builtin__)
959 959
960 960 # Put 'help' in the user namespace
961 961 try:
962 962 from site import _Helper
963 963 ns['help'] = _Helper()
964 964 except ImportError:
965 965 warn('help() not available - check site.py')
966 966
967 967 # make global variables for user access to the histories
968 968 ns['_ih'] = self.history_manager.input_hist_parsed
969 969 ns['_oh'] = self.history_manager.output_hist
970 970 ns['_dh'] = self.history_manager.dir_hist
971 971
972 972 ns['_sh'] = shadowns
973 973
974 974 # user aliases to input and output histories. These shouldn't show up
975 975 # in %who, as they can have very large reprs.
976 976 ns['In'] = self.history_manager.input_hist_parsed
977 977 ns['Out'] = self.history_manager.output_hist
978 978
979 979 # Store myself as the public api!!!
980 980 ns['get_ipython'] = self.get_ipython
981 981
982 982 # Sync what we've added so far to user_ns_hidden so these aren't seen
983 983 # by %who
984 984 self.user_ns_hidden.update(ns)
985 985
986 986 # Anything put into ns now would show up in %who. Think twice before
987 987 # putting anything here, as we really want %who to show the user their
988 988 # stuff, not our variables.
989 989
990 990 # Finally, update the real user's namespace
991 991 self.user_ns.update(ns)
992 992
993 def reset(self):
993 def reset(self, new_session=True):
994 994 """Clear all internal namespaces.
995 995
996 996 Note that this is much more aggressive than %reset, since it clears
997 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 1001 # Clear histories
1000 self.history_manager.reset()
1002 self.history_manager.reset(new_session)
1001 1003
1002 1004 # Reset counter used to index all histories
1003 1005 self.execution_count = 0
1004 1006
1005 1007 # Restore the user namespaces to minimal usability
1006 1008 for ns in self.ns_refs_table:
1007 1009 ns.clear()
1008 1010
1009 1011 # The main execution namespaces must be cleared very carefully,
1010 1012 # skipping the deletion of the builtin-related keys, because doing so
1011 1013 # would cause errors in many object's __del__ methods.
1012 1014 for ns in [self.user_ns, self.user_global_ns]:
1013 1015 drop_keys = set(ns.keys())
1014 1016 drop_keys.discard('__builtin__')
1015 1017 drop_keys.discard('__builtins__')
1016 1018 for k in drop_keys:
1017 1019 del ns[k]
1018 1020
1019 1021 # Restore the user namespaces to minimal usability
1020 1022 self.init_user_ns()
1021 1023
1022 1024 # Restore the default and user aliases
1023 1025 self.alias_manager.clear_aliases()
1024 1026 self.alias_manager.init_aliases()
1025 1027
1026 1028 def reset_selective(self, regex=None):
1027 1029 """Clear selective variables from internal namespaces based on a
1028 1030 specified regular expression.
1029 1031
1030 1032 Parameters
1031 1033 ----------
1032 1034 regex : string or compiled pattern, optional
1033 1035 A regular expression pattern that will be used in searching
1034 1036 variable names in the users namespaces.
1035 1037 """
1036 1038 if regex is not None:
1037 1039 try:
1038 1040 m = re.compile(regex)
1039 1041 except TypeError:
1040 1042 raise TypeError('regex must be a string or compiled pattern')
1041 1043 # Search for keys in each namespace that match the given regex
1042 1044 # If a match is found, delete the key/value pair.
1043 1045 for ns in self.ns_refs_table:
1044 1046 for var in ns:
1045 1047 if m.search(var):
1046 1048 del ns[var]
1047 1049
1048 1050 def push(self, variables, interactive=True):
1049 1051 """Inject a group of variables into the IPython user namespace.
1050 1052
1051 1053 Parameters
1052 1054 ----------
1053 1055 variables : dict, str or list/tuple of str
1054 1056 The variables to inject into the user's namespace. If a dict, a
1055 1057 simple update is done. If a str, the string is assumed to have
1056 1058 variable names separated by spaces. A list/tuple of str can also
1057 1059 be used to give the variable names. If just the variable names are
1058 1060 give (list/tuple/str) then the variable values looked up in the
1059 1061 callers frame.
1060 1062 interactive : bool
1061 1063 If True (default), the variables will be listed with the ``who``
1062 1064 magic.
1063 1065 """
1064 1066 vdict = None
1065 1067
1066 1068 # We need a dict of name/value pairs to do namespace updates.
1067 1069 if isinstance(variables, dict):
1068 1070 vdict = variables
1069 1071 elif isinstance(variables, (basestring, list, tuple)):
1070 1072 if isinstance(variables, basestring):
1071 1073 vlist = variables.split()
1072 1074 else:
1073 1075 vlist = variables
1074 1076 vdict = {}
1075 1077 cf = sys._getframe(1)
1076 1078 for name in vlist:
1077 1079 try:
1078 1080 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1079 1081 except:
1080 1082 print ('Could not get variable %s from %s' %
1081 1083 (name,cf.f_code.co_name))
1082 1084 else:
1083 1085 raise ValueError('variables must be a dict/str/list/tuple')
1084 1086
1085 1087 # Propagate variables to user namespace
1086 1088 self.user_ns.update(vdict)
1087 1089
1088 1090 # And configure interactive visibility
1089 1091 config_ns = self.user_ns_hidden
1090 1092 if interactive:
1091 1093 for name, val in vdict.iteritems():
1092 1094 config_ns.pop(name, None)
1093 1095 else:
1094 1096 for name,val in vdict.iteritems():
1095 1097 config_ns[name] = val
1096 1098
1097 1099 #-------------------------------------------------------------------------
1098 1100 # Things related to object introspection
1099 1101 #-------------------------------------------------------------------------
1100 1102
1101 1103 def _ofind(self, oname, namespaces=None):
1102 1104 """Find an object in the available namespaces.
1103 1105
1104 1106 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1105 1107
1106 1108 Has special code to detect magic functions.
1107 1109 """
1108 1110 #oname = oname.strip()
1109 1111 #print '1- oname: <%r>' % oname # dbg
1110 1112 try:
1111 1113 oname = oname.strip().encode('ascii')
1112 1114 #print '2- oname: <%r>' % oname # dbg
1113 1115 except UnicodeEncodeError:
1114 1116 print 'Python identifiers can only contain ascii characters.'
1115 1117 return dict(found=False)
1116 1118
1117 1119 alias_ns = None
1118 1120 if namespaces is None:
1119 1121 # Namespaces to search in:
1120 1122 # Put them in a list. The order is important so that we
1121 1123 # find things in the same order that Python finds them.
1122 1124 namespaces = [ ('Interactive', self.user_ns),
1123 1125 ('IPython internal', self.internal_ns),
1124 1126 ('Python builtin', __builtin__.__dict__),
1125 1127 ('Alias', self.alias_manager.alias_table),
1126 1128 ]
1127 1129 alias_ns = self.alias_manager.alias_table
1128 1130
1129 1131 # initialize results to 'null'
1130 1132 found = False; obj = None; ospace = None; ds = None;
1131 1133 ismagic = False; isalias = False; parent = None
1132 1134
1133 1135 # We need to special-case 'print', which as of python2.6 registers as a
1134 1136 # function but should only be treated as one if print_function was
1135 1137 # loaded with a future import. In this case, just bail.
1136 1138 if (oname == 'print' and not (self.compile.compiler_flags &
1137 1139 __future__.CO_FUTURE_PRINT_FUNCTION)):
1138 1140 return {'found':found, 'obj':obj, 'namespace':ospace,
1139 1141 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1140 1142
1141 1143 # Look for the given name by splitting it in parts. If the head is
1142 1144 # found, then we look for all the remaining parts as members, and only
1143 1145 # declare success if we can find them all.
1144 1146 oname_parts = oname.split('.')
1145 1147 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1146 1148 for nsname,ns in namespaces:
1147 1149 try:
1148 1150 obj = ns[oname_head]
1149 1151 except KeyError:
1150 1152 continue
1151 1153 else:
1152 1154 #print 'oname_rest:', oname_rest # dbg
1153 1155 for part in oname_rest:
1154 1156 try:
1155 1157 parent = obj
1156 1158 obj = getattr(obj,part)
1157 1159 except:
1158 1160 # Blanket except b/c some badly implemented objects
1159 1161 # allow __getattr__ to raise exceptions other than
1160 1162 # AttributeError, which then crashes IPython.
1161 1163 break
1162 1164 else:
1163 1165 # If we finish the for loop (no break), we got all members
1164 1166 found = True
1165 1167 ospace = nsname
1166 1168 if ns == alias_ns:
1167 1169 isalias = True
1168 1170 break # namespace loop
1169 1171
1170 1172 # Try to see if it's magic
1171 1173 if not found:
1172 1174 if oname.startswith(ESC_MAGIC):
1173 1175 oname = oname[1:]
1174 1176 obj = getattr(self,'magic_'+oname,None)
1175 1177 if obj is not None:
1176 1178 found = True
1177 1179 ospace = 'IPython internal'
1178 1180 ismagic = True
1179 1181
1180 1182 # Last try: special-case some literals like '', [], {}, etc:
1181 1183 if not found and oname_head in ["''",'""','[]','{}','()']:
1182 1184 obj = eval(oname_head)
1183 1185 found = True
1184 1186 ospace = 'Interactive'
1185 1187
1186 1188 return {'found':found, 'obj':obj, 'namespace':ospace,
1187 1189 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1188 1190
1189 1191 def _ofind_property(self, oname, info):
1190 1192 """Second part of object finding, to look for property details."""
1191 1193 if info.found:
1192 1194 # Get the docstring of the class property if it exists.
1193 1195 path = oname.split('.')
1194 1196 root = '.'.join(path[:-1])
1195 1197 if info.parent is not None:
1196 1198 try:
1197 1199 target = getattr(info.parent, '__class__')
1198 1200 # The object belongs to a class instance.
1199 1201 try:
1200 1202 target = getattr(target, path[-1])
1201 1203 # The class defines the object.
1202 1204 if isinstance(target, property):
1203 1205 oname = root + '.__class__.' + path[-1]
1204 1206 info = Struct(self._ofind(oname))
1205 1207 except AttributeError: pass
1206 1208 except AttributeError: pass
1207 1209
1208 1210 # We return either the new info or the unmodified input if the object
1209 1211 # hadn't been found
1210 1212 return info
1211 1213
1212 1214 def _object_find(self, oname, namespaces=None):
1213 1215 """Find an object and return a struct with info about it."""
1214 1216 inf = Struct(self._ofind(oname, namespaces))
1215 1217 return Struct(self._ofind_property(oname, inf))
1216 1218
1217 1219 def _inspect(self, meth, oname, namespaces=None, **kw):
1218 1220 """Generic interface to the inspector system.
1219 1221
1220 1222 This function is meant to be called by pdef, pdoc & friends."""
1221 1223 info = self._object_find(oname)
1222 1224 if info.found:
1223 1225 pmethod = getattr(self.inspector, meth)
1224 1226 formatter = format_screen if info.ismagic else None
1225 1227 if meth == 'pdoc':
1226 1228 pmethod(info.obj, oname, formatter)
1227 1229 elif meth == 'pinfo':
1228 1230 pmethod(info.obj, oname, formatter, info, **kw)
1229 1231 else:
1230 1232 pmethod(info.obj, oname)
1231 1233 else:
1232 1234 print 'Object `%s` not found.' % oname
1233 1235 return 'not found' # so callers can take other action
1234 1236
1235 1237 def object_inspect(self, oname):
1236 1238 info = self._object_find(oname)
1237 1239 if info.found:
1238 1240 return self.inspector.info(info.obj, oname, info=info)
1239 1241 else:
1240 1242 return oinspect.object_info(name=oname, found=False)
1241 1243
1242 1244 #-------------------------------------------------------------------------
1243 1245 # Things related to history management
1244 1246 #-------------------------------------------------------------------------
1245 1247
1246 1248 def init_history(self):
1247 1249 """Sets up the command history, and starts regular autosaves."""
1248 1250 self.history_manager = HistoryManager(shell=self, config=self.config)
1249 1251
1250 1252 def history_saving_wrapper(self, func):
1251 1253 """ Wrap func for readline history saving
1252 1254
1253 1255 Convert func into callable that saves & restores
1254 1256 history around the call """
1255 1257
1256 1258 if self.has_readline:
1257 1259 from IPython.utils import rlineimpl as readline
1258 1260 else:
1259 1261 return func
1260 1262
1261 1263 def wrapper():
1262 1264 self.save_history()
1263 1265 try:
1264 1266 func()
1265 1267 finally:
1266 1268 self.reload_history()
1267 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 1273 # Things related to exception handling and tracebacks (not debugging)
1275 1274 #-------------------------------------------------------------------------
1276 1275
1277 1276 def init_traceback_handlers(self, custom_exceptions):
1278 1277 # Syntax error handler.
1279 1278 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1280 1279
1281 1280 # The interactive one is initialized with an offset, meaning we always
1282 1281 # want to remove the topmost item in the traceback, which is our own
1283 1282 # internal code. Valid modes: ['Plain','Context','Verbose']
1284 1283 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1285 1284 color_scheme='NoColor',
1286 1285 tb_offset = 1,
1287 1286 check_cache=self.compile.check_cache)
1288 1287
1289 1288 # The instance will store a pointer to the system-wide exception hook,
1290 1289 # so that runtime code (such as magics) can access it. This is because
1291 1290 # during the read-eval loop, it may get temporarily overwritten.
1292 1291 self.sys_excepthook = sys.excepthook
1293 1292
1294 1293 # and add any custom exception handlers the user may have specified
1295 1294 self.set_custom_exc(*custom_exceptions)
1296 1295
1297 1296 # Set the exception mode
1298 1297 self.InteractiveTB.set_mode(mode=self.xmode)
1299 1298
1300 1299 def set_custom_exc(self, exc_tuple, handler):
1301 1300 """set_custom_exc(exc_tuple,handler)
1302 1301
1303 1302 Set a custom exception handler, which will be called if any of the
1304 1303 exceptions in exc_tuple occur in the mainloop (specifically, in the
1305 1304 run_code() method.
1306 1305
1307 1306 Inputs:
1308 1307
1309 1308 - exc_tuple: a *tuple* of valid exceptions to call the defined
1310 1309 handler for. It is very important that you use a tuple, and NOT A
1311 1310 LIST here, because of the way Python's except statement works. If
1312 1311 you only want to trap a single exception, use a singleton tuple:
1313 1312
1314 1313 exc_tuple == (MyCustomException,)
1315 1314
1316 1315 - handler: this must be defined as a function with the following
1317 1316 basic interface::
1318 1317
1319 1318 def my_handler(self, etype, value, tb, tb_offset=None)
1320 1319 ...
1321 1320 # The return value must be
1322 1321 return structured_traceback
1323 1322
1324 1323 This will be made into an instance method (via types.MethodType)
1325 1324 of IPython itself, and it will be called if any of the exceptions
1326 1325 listed in the exc_tuple are caught. If the handler is None, an
1327 1326 internal basic one is used, which just prints basic info.
1328 1327
1329 1328 WARNING: by putting in your own exception handler into IPython's main
1330 1329 execution loop, you run a very good chance of nasty crashes. This
1331 1330 facility should only be used if you really know what you are doing."""
1332 1331
1333 1332 assert type(exc_tuple)==type(()) , \
1334 1333 "The custom exceptions must be given AS A TUPLE."
1335 1334
1336 1335 def dummy_handler(self,etype,value,tb):
1337 1336 print '*** Simple custom exception handler ***'
1338 1337 print 'Exception type :',etype
1339 1338 print 'Exception value:',value
1340 1339 print 'Traceback :',tb
1341 1340 print 'Source code :','\n'.join(self.buffer)
1342 1341
1343 1342 if handler is None: handler = dummy_handler
1344 1343
1345 1344 self.CustomTB = types.MethodType(handler,self)
1346 1345 self.custom_exceptions = exc_tuple
1347 1346
1348 1347 def excepthook(self, etype, value, tb):
1349 1348 """One more defense for GUI apps that call sys.excepthook.
1350 1349
1351 1350 GUI frameworks like wxPython trap exceptions and call
1352 1351 sys.excepthook themselves. I guess this is a feature that
1353 1352 enables them to keep running after exceptions that would
1354 1353 otherwise kill their mainloop. This is a bother for IPython
1355 1354 which excepts to catch all of the program exceptions with a try:
1356 1355 except: statement.
1357 1356
1358 1357 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1359 1358 any app directly invokes sys.excepthook, it will look to the user like
1360 1359 IPython crashed. In order to work around this, we can disable the
1361 1360 CrashHandler and replace it with this excepthook instead, which prints a
1362 1361 regular traceback using our InteractiveTB. In this fashion, apps which
1363 1362 call sys.excepthook will generate a regular-looking exception from
1364 1363 IPython, and the CrashHandler will only be triggered by real IPython
1365 1364 crashes.
1366 1365
1367 1366 This hook should be used sparingly, only in places which are not likely
1368 1367 to be true IPython errors.
1369 1368 """
1370 1369 self.showtraceback((etype,value,tb),tb_offset=0)
1371 1370
1372 1371 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1373 1372 exception_only=False):
1374 1373 """Display the exception that just occurred.
1375 1374
1376 1375 If nothing is known about the exception, this is the method which
1377 1376 should be used throughout the code for presenting user tracebacks,
1378 1377 rather than directly invoking the InteractiveTB object.
1379 1378
1380 1379 A specific showsyntaxerror() also exists, but this method can take
1381 1380 care of calling it if needed, so unless you are explicitly catching a
1382 1381 SyntaxError exception, don't try to analyze the stack manually and
1383 1382 simply call this method."""
1384 1383
1385 1384 try:
1386 1385 if exc_tuple is None:
1387 1386 etype, value, tb = sys.exc_info()
1388 1387 else:
1389 1388 etype, value, tb = exc_tuple
1390 1389
1391 1390 if etype is None:
1392 1391 if hasattr(sys, 'last_type'):
1393 1392 etype, value, tb = sys.last_type, sys.last_value, \
1394 1393 sys.last_traceback
1395 1394 else:
1396 1395 self.write_err('No traceback available to show.\n')
1397 1396 return
1398 1397
1399 1398 if etype is SyntaxError:
1400 1399 # Though this won't be called by syntax errors in the input
1401 1400 # line, there may be SyntaxError cases whith imported code.
1402 1401 self.showsyntaxerror(filename)
1403 1402 elif etype is UsageError:
1404 1403 print "UsageError:", value
1405 1404 else:
1406 1405 # WARNING: these variables are somewhat deprecated and not
1407 1406 # necessarily safe to use in a threaded environment, but tools
1408 1407 # like pdb depend on their existence, so let's set them. If we
1409 1408 # find problems in the field, we'll need to revisit their use.
1410 1409 sys.last_type = etype
1411 1410 sys.last_value = value
1412 1411 sys.last_traceback = tb
1413 1412
1414 1413 if etype in self.custom_exceptions:
1415 1414 # FIXME: Old custom traceback objects may just return a
1416 1415 # string, in that case we just put it into a list
1417 1416 stb = self.CustomTB(etype, value, tb, tb_offset)
1418 1417 if isinstance(ctb, basestring):
1419 1418 stb = [stb]
1420 1419 else:
1421 1420 if exception_only:
1422 1421 stb = ['An exception has occurred, use %tb to see '
1423 1422 'the full traceback.\n']
1424 1423 stb.extend(self.InteractiveTB.get_exception_only(etype,
1425 1424 value))
1426 1425 else:
1427 1426 stb = self.InteractiveTB.structured_traceback(etype,
1428 1427 value, tb, tb_offset=tb_offset)
1429 1428 # FIXME: the pdb calling should be done by us, not by
1430 1429 # the code computing the traceback.
1431 1430 if self.InteractiveTB.call_pdb:
1432 1431 # pdb mucks up readline, fix it back
1433 1432 self.set_readline_completer()
1434 1433
1435 1434 # Actually show the traceback
1436 1435 self._showtraceback(etype, value, stb)
1437 1436
1438 1437 except KeyboardInterrupt:
1439 1438 self.write_err("\nKeyboardInterrupt\n")
1440 1439
1441 1440 def _showtraceback(self, etype, evalue, stb):
1442 1441 """Actually show a traceback.
1443 1442
1444 1443 Subclasses may override this method to put the traceback on a different
1445 1444 place, like a side channel.
1446 1445 """
1447 1446 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1448 1447
1449 1448 def showsyntaxerror(self, filename=None):
1450 1449 """Display the syntax error that just occurred.
1451 1450
1452 1451 This doesn't display a stack trace because there isn't one.
1453 1452
1454 1453 If a filename is given, it is stuffed in the exception instead
1455 1454 of what was there before (because Python's parser always uses
1456 1455 "<string>" when reading from a string).
1457 1456 """
1458 1457 etype, value, last_traceback = sys.exc_info()
1459 1458
1460 1459 # See note about these variables in showtraceback() above
1461 1460 sys.last_type = etype
1462 1461 sys.last_value = value
1463 1462 sys.last_traceback = last_traceback
1464 1463
1465 1464 if filename and etype is SyntaxError:
1466 1465 # Work hard to stuff the correct filename in the exception
1467 1466 try:
1468 1467 msg, (dummy_filename, lineno, offset, line) = value
1469 1468 except:
1470 1469 # Not the format we expect; leave it alone
1471 1470 pass
1472 1471 else:
1473 1472 # Stuff in the right filename
1474 1473 try:
1475 1474 # Assume SyntaxError is a class exception
1476 1475 value = SyntaxError(msg, (filename, lineno, offset, line))
1477 1476 except:
1478 1477 # If that failed, assume SyntaxError is a string
1479 1478 value = msg, (filename, lineno, offset, line)
1480 1479 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1481 1480 self._showtraceback(etype, value, stb)
1482 1481
1483 1482 #-------------------------------------------------------------------------
1484 1483 # Things related to readline
1485 1484 #-------------------------------------------------------------------------
1486 1485
1487 1486 def init_readline(self):
1488 1487 """Command history completion/saving/reloading."""
1489 1488
1490 1489 if self.readline_use:
1491 1490 import IPython.utils.rlineimpl as readline
1492 1491
1493 1492 self.rl_next_input = None
1494 1493 self.rl_do_indent = False
1495 1494
1496 1495 if not self.readline_use or not readline.have_readline:
1497 1496 self.has_readline = False
1498 1497 self.readline = None
1499 1498 # Set a number of methods that depend on readline to be no-op
1500 1499 self.set_readline_completer = no_op
1501 1500 self.set_custom_completer = no_op
1502 1501 self.set_completer_frame = no_op
1503 1502 warn('Readline services not available or not loaded.')
1504 1503 else:
1505 1504 self.has_readline = True
1506 1505 self.readline = readline
1507 1506 sys.modules['readline'] = readline
1508 1507
1509 1508 # Platform-specific configuration
1510 1509 if os.name == 'nt':
1511 1510 # FIXME - check with Frederick to see if we can harmonize
1512 1511 # naming conventions with pyreadline to avoid this
1513 1512 # platform-dependent check
1514 1513 self.readline_startup_hook = readline.set_pre_input_hook
1515 1514 else:
1516 1515 self.readline_startup_hook = readline.set_startup_hook
1517 1516
1518 1517 # Load user's initrc file (readline config)
1519 1518 # Or if libedit is used, load editrc.
1520 1519 inputrc_name = os.environ.get('INPUTRC')
1521 1520 if inputrc_name is None:
1522 1521 home_dir = get_home_dir()
1523 1522 if home_dir is not None:
1524 1523 inputrc_name = '.inputrc'
1525 1524 if readline.uses_libedit:
1526 1525 inputrc_name = '.editrc'
1527 1526 inputrc_name = os.path.join(home_dir, inputrc_name)
1528 1527 if os.path.isfile(inputrc_name):
1529 1528 try:
1530 1529 readline.read_init_file(inputrc_name)
1531 1530 except:
1532 1531 warn('Problems reading readline initialization file <%s>'
1533 1532 % inputrc_name)
1534 1533
1535 1534 # Configure readline according to user's prefs
1536 1535 # This is only done if GNU readline is being used. If libedit
1537 1536 # is being used (as on Leopard) the readline config is
1538 1537 # not run as the syntax for libedit is different.
1539 1538 if not readline.uses_libedit:
1540 1539 for rlcommand in self.readline_parse_and_bind:
1541 1540 #print "loading rl:",rlcommand # dbg
1542 1541 readline.parse_and_bind(rlcommand)
1543 1542
1544 1543 # Remove some chars from the delimiters list. If we encounter
1545 1544 # unicode chars, discard them.
1546 1545 delims = readline.get_completer_delims().encode("ascii", "ignore")
1547 1546 delims = delims.translate(None, self.readline_remove_delims)
1548 1547 delims = delims.replace(ESC_MAGIC, '')
1549 1548 readline.set_completer_delims(delims)
1550 1549 # otherwise we end up with a monster history after a while:
1551 1550 readline.set_history_length(self.history_length)
1552 1551
1553 1552 # Load the last 1000 lines from history
1554 1553 for _, _, cell in self.history_manager.get_hist_tail(1000):
1555 1554 if cell.strip(): # Ignore blank lines
1556 1555 for line in cell.splitlines():
1557 1556 readline.add_history(line)
1558 1557
1559 1558 # Configure auto-indent for all platforms
1560 1559 self.set_autoindent(self.autoindent)
1561 1560
1562 1561 def set_next_input(self, s):
1563 1562 """ Sets the 'default' input string for the next command line.
1564 1563
1565 1564 Requires readline.
1566 1565
1567 1566 Example:
1568 1567
1569 1568 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1570 1569 [D:\ipython]|2> Hello Word_ # cursor is here
1571 1570 """
1572 1571
1573 1572 self.rl_next_input = s
1574 1573
1575 1574 # Maybe move this to the terminal subclass?
1576 1575 def pre_readline(self):
1577 1576 """readline hook to be used at the start of each line.
1578 1577
1579 1578 Currently it handles auto-indent only."""
1580 1579
1581 1580 if self.rl_do_indent:
1582 1581 self.readline.insert_text(self._indent_current_str())
1583 1582 if self.rl_next_input is not None:
1584 1583 self.readline.insert_text(self.rl_next_input)
1585 1584 self.rl_next_input = None
1586 1585
1587 1586 def _indent_current_str(self):
1588 1587 """return the current level of indentation as a string"""
1589 1588 return self.input_splitter.indent_spaces * ' '
1590 1589
1591 1590 #-------------------------------------------------------------------------
1592 1591 # Things related to text completion
1593 1592 #-------------------------------------------------------------------------
1594 1593
1595 1594 def init_completer(self):
1596 1595 """Initialize the completion machinery.
1597 1596
1598 1597 This creates completion machinery that can be used by client code,
1599 1598 either interactively in-process (typically triggered by the readline
1600 1599 library), programatically (such as in test suites) or out-of-prcess
1601 1600 (typically over the network by remote frontends).
1602 1601 """
1603 1602 from IPython.core.completer import IPCompleter
1604 1603 from IPython.core.completerlib import (module_completer,
1605 1604 magic_run_completer, cd_completer)
1606 1605
1607 1606 self.Completer = IPCompleter(self,
1608 1607 self.user_ns,
1609 1608 self.user_global_ns,
1610 1609 self.readline_omit__names,
1611 1610 self.alias_manager.alias_table,
1612 1611 self.has_readline)
1613 1612
1614 1613 # Add custom completers to the basic ones built into IPCompleter
1615 1614 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1616 1615 self.strdispatchers['complete_command'] = sdisp
1617 1616 self.Completer.custom_completers = sdisp
1618 1617
1619 1618 self.set_hook('complete_command', module_completer, str_key = 'import')
1620 1619 self.set_hook('complete_command', module_completer, str_key = 'from')
1621 1620 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1622 1621 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1623 1622
1624 1623 # Only configure readline if we truly are using readline. IPython can
1625 1624 # do tab-completion over the network, in GUIs, etc, where readline
1626 1625 # itself may be absent
1627 1626 if self.has_readline:
1628 1627 self.set_readline_completer()
1629 1628
1630 1629 def complete(self, text, line=None, cursor_pos=None):
1631 1630 """Return the completed text and a list of completions.
1632 1631
1633 1632 Parameters
1634 1633 ----------
1635 1634
1636 1635 text : string
1637 1636 A string of text to be completed on. It can be given as empty and
1638 1637 instead a line/position pair are given. In this case, the
1639 1638 completer itself will split the line like readline does.
1640 1639
1641 1640 line : string, optional
1642 1641 The complete line that text is part of.
1643 1642
1644 1643 cursor_pos : int, optional
1645 1644 The position of the cursor on the input line.
1646 1645
1647 1646 Returns
1648 1647 -------
1649 1648 text : string
1650 1649 The actual text that was completed.
1651 1650
1652 1651 matches : list
1653 1652 A sorted list with all possible completions.
1654 1653
1655 1654 The optional arguments allow the completion to take more context into
1656 1655 account, and are part of the low-level completion API.
1657 1656
1658 1657 This is a wrapper around the completion mechanism, similar to what
1659 1658 readline does at the command line when the TAB key is hit. By
1660 1659 exposing it as a method, it can be used by other non-readline
1661 1660 environments (such as GUIs) for text completion.
1662 1661
1663 1662 Simple usage example:
1664 1663
1665 1664 In [1]: x = 'hello'
1666 1665
1667 1666 In [2]: _ip.complete('x.l')
1668 1667 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1669 1668 """
1670 1669
1671 1670 # Inject names into __builtin__ so we can complete on the added names.
1672 1671 with self.builtin_trap:
1673 1672 return self.Completer.complete(text, line, cursor_pos)
1674 1673
1675 1674 def set_custom_completer(self, completer, pos=0):
1676 1675 """Adds a new custom completer function.
1677 1676
1678 1677 The position argument (defaults to 0) is the index in the completers
1679 1678 list where you want the completer to be inserted."""
1680 1679
1681 1680 newcomp = types.MethodType(completer,self.Completer)
1682 1681 self.Completer.matchers.insert(pos,newcomp)
1683 1682
1684 1683 def set_readline_completer(self):
1685 1684 """Reset readline's completer to be our own."""
1686 1685 self.readline.set_completer(self.Completer.rlcomplete)
1687 1686
1688 1687 def set_completer_frame(self, frame=None):
1689 1688 """Set the frame of the completer."""
1690 1689 if frame:
1691 1690 self.Completer.namespace = frame.f_locals
1692 1691 self.Completer.global_namespace = frame.f_globals
1693 1692 else:
1694 1693 self.Completer.namespace = self.user_ns
1695 1694 self.Completer.global_namespace = self.user_global_ns
1696 1695
1697 1696 #-------------------------------------------------------------------------
1698 1697 # Things related to magics
1699 1698 #-------------------------------------------------------------------------
1700 1699
1701 1700 def init_magics(self):
1702 1701 # FIXME: Move the color initialization to the DisplayHook, which
1703 1702 # should be split into a prompt manager and displayhook. We probably
1704 1703 # even need a centralize colors management object.
1705 1704 self.magic_colors(self.colors)
1706 1705 # History was moved to a separate module
1707 1706 from . import history
1708 1707 history.init_ipython(self)
1709 1708
1710 1709 def magic(self,arg_s):
1711 1710 """Call a magic function by name.
1712 1711
1713 1712 Input: a string containing the name of the magic function to call and
1714 1713 any additional arguments to be passed to the magic.
1715 1714
1716 1715 magic('name -opt foo bar') is equivalent to typing at the ipython
1717 1716 prompt:
1718 1717
1719 1718 In[1]: %name -opt foo bar
1720 1719
1721 1720 To call a magic without arguments, simply use magic('name').
1722 1721
1723 1722 This provides a proper Python function to call IPython's magics in any
1724 1723 valid Python code you can type at the interpreter, including loops and
1725 1724 compound statements.
1726 1725 """
1727 1726 args = arg_s.split(' ',1)
1728 1727 magic_name = args[0]
1729 1728 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1730 1729
1731 1730 try:
1732 1731 magic_args = args[1]
1733 1732 except IndexError:
1734 1733 magic_args = ''
1735 1734 fn = getattr(self,'magic_'+magic_name,None)
1736 1735 if fn is None:
1737 1736 error("Magic function `%s` not found." % magic_name)
1738 1737 else:
1739 1738 magic_args = self.var_expand(magic_args,1)
1740 1739 with nested(self.builtin_trap,):
1741 1740 result = fn(magic_args)
1742 1741 return result
1743 1742
1744 1743 def define_magic(self, magicname, func):
1745 1744 """Expose own function as magic function for ipython
1746 1745
1747 1746 def foo_impl(self,parameter_s=''):
1748 1747 'My very own magic!. (Use docstrings, IPython reads them).'
1749 1748 print 'Magic function. Passed parameter is between < >:'
1750 1749 print '<%s>' % parameter_s
1751 1750 print 'The self object is:',self
1752 1751
1753 1752 self.define_magic('foo',foo_impl)
1754 1753 """
1755 1754
1756 1755 import new
1757 1756 im = types.MethodType(func,self)
1758 1757 old = getattr(self, "magic_" + magicname, None)
1759 1758 setattr(self, "magic_" + magicname, im)
1760 1759 return old
1761 1760
1762 1761 #-------------------------------------------------------------------------
1763 1762 # Things related to macros
1764 1763 #-------------------------------------------------------------------------
1765 1764
1766 1765 def define_macro(self, name, themacro):
1767 1766 """Define a new macro
1768 1767
1769 1768 Parameters
1770 1769 ----------
1771 1770 name : str
1772 1771 The name of the macro.
1773 1772 themacro : str or Macro
1774 1773 The action to do upon invoking the macro. If a string, a new
1775 1774 Macro object is created by passing the string to it.
1776 1775 """
1777 1776
1778 1777 from IPython.core import macro
1779 1778
1780 1779 if isinstance(themacro, basestring):
1781 1780 themacro = macro.Macro(themacro)
1782 1781 if not isinstance(themacro, macro.Macro):
1783 1782 raise ValueError('A macro must be a string or a Macro instance.')
1784 1783 self.user_ns[name] = themacro
1785 1784
1786 1785 #-------------------------------------------------------------------------
1787 1786 # Things related to the running of system commands
1788 1787 #-------------------------------------------------------------------------
1789 1788
1790 1789 def system(self, cmd):
1791 1790 """Call the given cmd in a subprocess.
1792 1791
1793 1792 Parameters
1794 1793 ----------
1795 1794 cmd : str
1796 1795 Command to execute (can not end in '&', as bacground processes are
1797 1796 not supported.
1798 1797 """
1799 1798 # We do not support backgrounding processes because we either use
1800 1799 # pexpect or pipes to read from. Users can always just call
1801 1800 # os.system() if they really want a background process.
1802 1801 if cmd.endswith('&'):
1803 1802 raise OSError("Background processes not supported.")
1804 1803
1805 1804 return system(self.var_expand(cmd, depth=2))
1806 1805
1807 1806 def getoutput(self, cmd, split=True):
1808 1807 """Get output (possibly including stderr) from a subprocess.
1809 1808
1810 1809 Parameters
1811 1810 ----------
1812 1811 cmd : str
1813 1812 Command to execute (can not end in '&', as background processes are
1814 1813 not supported.
1815 1814 split : bool, optional
1816 1815
1817 1816 If True, split the output into an IPython SList. Otherwise, an
1818 1817 IPython LSString is returned. These are objects similar to normal
1819 1818 lists and strings, with a few convenience attributes for easier
1820 1819 manipulation of line-based output. You can use '?' on them for
1821 1820 details.
1822 1821 """
1823 1822 if cmd.endswith('&'):
1824 1823 raise OSError("Background processes not supported.")
1825 1824 out = getoutput(self.var_expand(cmd, depth=2))
1826 1825 if split:
1827 1826 out = SList(out.splitlines())
1828 1827 else:
1829 1828 out = LSString(out)
1830 1829 return out
1831 1830
1832 1831 #-------------------------------------------------------------------------
1833 1832 # Things related to aliases
1834 1833 #-------------------------------------------------------------------------
1835 1834
1836 1835 def init_alias(self):
1837 1836 self.alias_manager = AliasManager(shell=self, config=self.config)
1838 1837 self.ns_table['alias'] = self.alias_manager.alias_table,
1839 1838
1840 1839 #-------------------------------------------------------------------------
1841 1840 # Things related to extensions and plugins
1842 1841 #-------------------------------------------------------------------------
1843 1842
1844 1843 def init_extension_manager(self):
1845 1844 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1846 1845
1847 1846 def init_plugin_manager(self):
1848 1847 self.plugin_manager = PluginManager(config=self.config)
1849 1848
1850 1849 #-------------------------------------------------------------------------
1851 1850 # Things related to payloads
1852 1851 #-------------------------------------------------------------------------
1853 1852
1854 1853 def init_payload(self):
1855 1854 self.payload_manager = PayloadManager(config=self.config)
1856 1855
1857 1856 #-------------------------------------------------------------------------
1858 1857 # Things related to the prefilter
1859 1858 #-------------------------------------------------------------------------
1860 1859
1861 1860 def init_prefilter(self):
1862 1861 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1863 1862 # Ultimately this will be refactored in the new interpreter code, but
1864 1863 # for now, we should expose the main prefilter method (there's legacy
1865 1864 # code out there that may rely on this).
1866 1865 self.prefilter = self.prefilter_manager.prefilter_lines
1867 1866
1868 1867 def auto_rewrite_input(self, cmd):
1869 1868 """Print to the screen the rewritten form of the user's command.
1870 1869
1871 1870 This shows visual feedback by rewriting input lines that cause
1872 1871 automatic calling to kick in, like::
1873 1872
1874 1873 /f x
1875 1874
1876 1875 into::
1877 1876
1878 1877 ------> f(x)
1879 1878
1880 1879 after the user's input prompt. This helps the user understand that the
1881 1880 input line was transformed automatically by IPython.
1882 1881 """
1883 1882 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1884 1883
1885 1884 try:
1886 1885 # plain ascii works better w/ pyreadline, on some machines, so
1887 1886 # we use it and only print uncolored rewrite if we have unicode
1888 1887 rw = str(rw)
1889 1888 print >> IPython.utils.io.Term.cout, rw
1890 1889 except UnicodeEncodeError:
1891 1890 print "------> " + cmd
1892 1891
1893 1892 #-------------------------------------------------------------------------
1894 1893 # Things related to extracting values/expressions from kernel and user_ns
1895 1894 #-------------------------------------------------------------------------
1896 1895
1897 1896 def _simple_error(self):
1898 1897 etype, value = sys.exc_info()[:2]
1899 1898 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1900 1899
1901 1900 def user_variables(self, names):
1902 1901 """Get a list of variable names from the user's namespace.
1903 1902
1904 1903 Parameters
1905 1904 ----------
1906 1905 names : list of strings
1907 1906 A list of names of variables to be read from the user namespace.
1908 1907
1909 1908 Returns
1910 1909 -------
1911 1910 A dict, keyed by the input names and with the repr() of each value.
1912 1911 """
1913 1912 out = {}
1914 1913 user_ns = self.user_ns
1915 1914 for varname in names:
1916 1915 try:
1917 1916 value = repr(user_ns[varname])
1918 1917 except:
1919 1918 value = self._simple_error()
1920 1919 out[varname] = value
1921 1920 return out
1922 1921
1923 1922 def user_expressions(self, expressions):
1924 1923 """Evaluate a dict of expressions in the user's namespace.
1925 1924
1926 1925 Parameters
1927 1926 ----------
1928 1927 expressions : dict
1929 1928 A dict with string keys and string values. The expression values
1930 1929 should be valid Python expressions, each of which will be evaluated
1931 1930 in the user namespace.
1932 1931
1933 1932 Returns
1934 1933 -------
1935 1934 A dict, keyed like the input expressions dict, with the repr() of each
1936 1935 value.
1937 1936 """
1938 1937 out = {}
1939 1938 user_ns = self.user_ns
1940 1939 global_ns = self.user_global_ns
1941 1940 for key, expr in expressions.iteritems():
1942 1941 try:
1943 1942 value = repr(eval(expr, global_ns, user_ns))
1944 1943 except:
1945 1944 value = self._simple_error()
1946 1945 out[key] = value
1947 1946 return out
1948 1947
1949 1948 #-------------------------------------------------------------------------
1950 1949 # Things related to the running of code
1951 1950 #-------------------------------------------------------------------------
1952 1951
1953 1952 def ex(self, cmd):
1954 1953 """Execute a normal python statement in user namespace."""
1955 1954 with nested(self.builtin_trap,):
1956 1955 exec cmd in self.user_global_ns, self.user_ns
1957 1956
1958 1957 def ev(self, expr):
1959 1958 """Evaluate python expression expr in user namespace.
1960 1959
1961 1960 Returns the result of evaluation
1962 1961 """
1963 1962 with nested(self.builtin_trap,):
1964 1963 return eval(expr, self.user_global_ns, self.user_ns)
1965 1964
1966 1965 def safe_execfile(self, fname, *where, **kw):
1967 1966 """A safe version of the builtin execfile().
1968 1967
1969 1968 This version will never throw an exception, but instead print
1970 1969 helpful error messages to the screen. This only works on pure
1971 1970 Python files with the .py extension.
1972 1971
1973 1972 Parameters
1974 1973 ----------
1975 1974 fname : string
1976 1975 The name of the file to be executed.
1977 1976 where : tuple
1978 1977 One or two namespaces, passed to execfile() as (globals,locals).
1979 1978 If only one is given, it is passed as both.
1980 1979 exit_ignore : bool (False)
1981 1980 If True, then silence SystemExit for non-zero status (it is always
1982 1981 silenced for zero status, as it is so common).
1983 1982 """
1984 1983 kw.setdefault('exit_ignore', False)
1985 1984
1986 1985 fname = os.path.abspath(os.path.expanduser(fname))
1987 1986
1988 1987 # Make sure we have a .py file
1989 1988 if not fname.endswith('.py'):
1990 1989 warn('File must end with .py to be run using execfile: <%s>' % fname)
1991 1990
1992 1991 # Make sure we can open the file
1993 1992 try:
1994 1993 with open(fname) as thefile:
1995 1994 pass
1996 1995 except:
1997 1996 warn('Could not open file <%s> for safe execution.' % fname)
1998 1997 return
1999 1998
2000 1999 # Find things also in current directory. This is needed to mimic the
2001 2000 # behavior of running a script from the system command line, where
2002 2001 # Python inserts the script's directory into sys.path
2003 2002 dname = os.path.dirname(fname)
2004 2003
2005 2004 with prepended_to_syspath(dname):
2006 2005 try:
2007 2006 execfile(fname,*where)
2008 2007 except SystemExit, status:
2009 2008 # If the call was made with 0 or None exit status (sys.exit(0)
2010 2009 # or sys.exit() ), don't bother showing a traceback, as both of
2011 2010 # these are considered normal by the OS:
2012 2011 # > python -c'import sys;sys.exit(0)'; echo $?
2013 2012 # 0
2014 2013 # > python -c'import sys;sys.exit()'; echo $?
2015 2014 # 0
2016 2015 # For other exit status, we show the exception unless
2017 2016 # explicitly silenced, but only in short form.
2018 2017 if status.code not in (0, None) and not kw['exit_ignore']:
2019 2018 self.showtraceback(exception_only=True)
2020 2019 except:
2021 2020 self.showtraceback()
2022 2021
2023 2022 def safe_execfile_ipy(self, fname):
2024 2023 """Like safe_execfile, but for .ipy files with IPython syntax.
2025 2024
2026 2025 Parameters
2027 2026 ----------
2028 2027 fname : str
2029 2028 The name of the file to execute. The filename must have a
2030 2029 .ipy extension.
2031 2030 """
2032 2031 fname = os.path.abspath(os.path.expanduser(fname))
2033 2032
2034 2033 # Make sure we have a .py file
2035 2034 if not fname.endswith('.ipy'):
2036 2035 warn('File must end with .py to be run using execfile: <%s>' % fname)
2037 2036
2038 2037 # Make sure we can open the file
2039 2038 try:
2040 2039 with open(fname) as thefile:
2041 2040 pass
2042 2041 except:
2043 2042 warn('Could not open file <%s> for safe execution.' % fname)
2044 2043 return
2045 2044
2046 2045 # Find things also in current directory. This is needed to mimic the
2047 2046 # behavior of running a script from the system command line, where
2048 2047 # Python inserts the script's directory into sys.path
2049 2048 dname = os.path.dirname(fname)
2050 2049
2051 2050 with prepended_to_syspath(dname):
2052 2051 try:
2053 2052 with open(fname) as thefile:
2054 2053 # self.run_cell currently captures all exceptions
2055 2054 # raised in user code. It would be nice if there were
2056 2055 # versions of runlines, execfile that did raise, so
2057 2056 # we could catch the errors.
2058 2057 self.run_cell(thefile.read())
2059 2058 except:
2060 2059 self.showtraceback()
2061 2060 warn('Unknown failure executing file: <%s>' % fname)
2062 2061
2063 2062 def run_cell(self, cell):
2064 2063 """Run the contents of an entire multiline 'cell' of code.
2065 2064
2066 2065 The cell is split into separate blocks which can be executed
2067 2066 individually. Then, based on how many blocks there are, they are
2068 2067 executed as follows:
2069 2068
2070 2069 - A single block: 'single' mode.
2071 2070
2072 2071 If there's more than one block, it depends:
2073 2072
2074 2073 - if the last one is no more than two lines long, run all but the last
2075 2074 in 'exec' mode and the very last one in 'single' mode. This makes it
2076 2075 easy to type simple expressions at the end to see computed values. -
2077 2076 otherwise (last one is also multiline), run all in 'exec' mode
2078 2077
2079 2078 When code is executed in 'single' mode, :func:`sys.displayhook` fires,
2080 2079 results are displayed and output prompts are computed. In 'exec' mode,
2081 2080 no results are displayed unless :func:`print` is called explicitly;
2082 2081 this mode is more akin to running a script.
2083 2082
2084 2083 Parameters
2085 2084 ----------
2086 2085 cell : str
2087 2086 A single or multiline string.
2088 2087 """
2089 2088
2090 2089 # We need to break up the input into executable blocks that can be run
2091 2090 # in 'single' mode, to provide comfortable user behavior.
2092 2091 blocks = self.input_splitter.split_blocks(cell)
2093 2092
2094 2093 if not blocks:
2095 2094 return
2096 2095
2097 2096 # Store the 'ipython' version of the cell as well, since that's what
2098 2097 # needs to go into the translated history and get executed (the
2099 2098 # original cell may contain non-python syntax).
2100 2099 ipy_cell = ''.join(blocks)
2101 2100
2102 2101 # Store raw and processed history
2103 2102 self.history_manager.store_inputs(self.execution_count, ipy_cell, cell)
2104 2103
2105 2104 self.logger.log(ipy_cell, cell)
2106 2105
2107 2106 # All user code execution must happen with our context managers active
2108 2107 with nested(self.builtin_trap, self.display_trap):
2109 2108
2110 2109 # Single-block input should behave like an interactive prompt
2111 2110 if len(blocks) == 1:
2112 2111 # since we return here, we need to update the execution count
2113 2112 out = self.run_one_block(blocks[0])
2114 2113 self.execution_count += 1
2115 2114 return out
2116 2115
2117 2116 # In multi-block input, if the last block is a simple (one-two
2118 2117 # lines) expression, run it in single mode so it produces output.
2119 2118 # Otherwise just feed the whole thing to run_code. This seems like
2120 2119 # a reasonable usability design.
2121 2120 last = blocks[-1]
2122 2121 last_nlines = len(last.splitlines())
2123 2122
2124 2123 # Note: below, whenever we call run_code, we must sync history
2125 2124 # ourselves, because run_code is NOT meant to manage history at all.
2126 2125 if last_nlines < 2:
2127 2126 # Here we consider the cell split between 'body' and 'last',
2128 2127 # store all history and execute 'body', and if successful, then
2129 2128 # proceed to execute 'last'.
2130 2129
2131 2130 # Get the main body to run as a cell
2132 2131 ipy_body = ''.join(blocks[:-1])
2133 2132 retcode = self.run_source(ipy_body, symbol='exec',
2134 2133 post_execute=False)
2135 2134 if retcode==0:
2136 2135 # And the last expression via runlines so it produces output
2137 2136 self.run_one_block(last)
2138 2137 else:
2139 2138 # Run the whole cell as one entity, storing both raw and
2140 2139 # processed input in history
2141 2140 self.run_source(ipy_cell, symbol='exec')
2142 2141
2143 2142 # Each cell is a *single* input, regardless of how many lines it has
2144 2143 self.execution_count += 1
2145 2144
2146 2145 def run_one_block(self, block):
2147 2146 """Run a single interactive block of source code.
2148 2147
2149 2148 If the block is single-line, dynamic transformations are applied to it
2150 2149 (like automagics, autocall and alias recognition).
2151 2150
2152 2151 If the block is multi-line, it must consist of valid Python code only.
2153 2152
2154 2153 Parameters
2155 2154 ----------
2156 2155 block : string
2157 2156 A (possibly multiline) string of code to be executed.
2158 2157
2159 2158 Returns
2160 2159 -------
2161 2160 The output of the underlying execution method used, be it
2162 2161 :meth:`run_source` or :meth:`run_single_line`.
2163 2162 """
2164 2163 if len(block.splitlines()) <= 1:
2165 2164 out = self.run_single_line(block)
2166 2165 else:
2167 2166 # Call run_source, which correctly compiles the input cell.
2168 2167 # run_code must only be called when we know we have a code object,
2169 2168 # as it does a naked exec and the compilation mode may not be what
2170 2169 # we wanted.
2171 2170 out = self.run_source(block)
2172 2171 return out
2173 2172
2174 2173 def run_single_line(self, line):
2175 2174 """Run a single-line interactive statement.
2176 2175
2177 2176 This assumes the input has been transformed to IPython syntax by
2178 2177 applying all static transformations (those with an explicit prefix like
2179 2178 % or !), but it will further try to apply the dynamic ones.
2180 2179
2181 2180 It does not update history.
2182 2181 """
2183 2182 tline = self.prefilter_manager.prefilter_line(line)
2184 2183 return self.run_source(tline)
2185 2184
2186 2185 # PENDING REMOVAL: this method is slated for deletion, once our new
2187 2186 # input logic has been 100% moved to frontends and is stable.
2188 2187 def runlines(self, lines, clean=False):
2189 2188 """Run a string of one or more lines of source.
2190 2189
2191 2190 This method is capable of running a string containing multiple source
2192 2191 lines, as if they had been entered at the IPython prompt. Since it
2193 2192 exposes IPython's processing machinery, the given strings can contain
2194 2193 magic calls (%magic), special shell access (!cmd), etc.
2195 2194 """
2196 2195
2197 if isinstance(lines, (list, tuple)):
2198 lines = '\n'.join(lines)
2196 if not isinstance(lines, (list, tuple)):
2197 lines = lines.splitlines()
2199 2198
2200 2199 if clean:
2201 2200 lines = self._cleanup_ipy_script(lines)
2202 2201
2203 2202 # We must start with a clean buffer, in case this is run from an
2204 2203 # interactive IPython session (via a magic, for example).
2205 2204 self.reset_buffer()
2206 lines = lines.splitlines()
2207 2205
2208 2206 # Since we will prefilter all lines, store the user's raw input too
2209 2207 # before we apply any transformations
2210 2208 self.buffer_raw[:] = [ l+'\n' for l in lines]
2211 2209
2212 2210 more = False
2213 2211 prefilter_lines = self.prefilter_manager.prefilter_lines
2214 2212 with nested(self.builtin_trap, self.display_trap):
2215 2213 for line in lines:
2216 2214 # skip blank lines so we don't mess up the prompt counter, but
2217 2215 # do NOT skip even a blank line if we are in a code block (more
2218 2216 # is true)
2219 2217
2220 2218 if line or more:
2221 2219 more = self.push_line(prefilter_lines(line, more))
2222 2220 # IPython's run_source returns None if there was an error
2223 2221 # compiling the code. This allows us to stop processing
2224 2222 # right away, so the user gets the error message at the
2225 2223 # right place.
2226 2224 if more is None:
2227 2225 break
2228 2226 # final newline in case the input didn't have it, so that the code
2229 2227 # actually does get executed
2230 2228 if more:
2231 2229 self.push_line('\n')
2232 2230
2233 2231 def run_source(self, source, filename=None,
2234 2232 symbol='single', post_execute=True):
2235 2233 """Compile and run some source in the interpreter.
2236 2234
2237 2235 Arguments are as for compile_command().
2238 2236
2239 2237 One several things can happen:
2240 2238
2241 2239 1) The input is incorrect; compile_command() raised an
2242 2240 exception (SyntaxError or OverflowError). A syntax traceback
2243 2241 will be printed by calling the showsyntaxerror() method.
2244 2242
2245 2243 2) The input is incomplete, and more input is required;
2246 2244 compile_command() returned None. Nothing happens.
2247 2245
2248 2246 3) The input is complete; compile_command() returned a code
2249 2247 object. The code is executed by calling self.run_code() (which
2250 2248 also handles run-time exceptions, except for SystemExit).
2251 2249
2252 2250 The return value is:
2253 2251
2254 2252 - True in case 2
2255 2253
2256 2254 - False in the other cases, unless an exception is raised, where
2257 2255 None is returned instead. This can be used by external callers to
2258 2256 know whether to continue feeding input or not.
2259 2257
2260 2258 The return value can be used to decide whether to use sys.ps1 or
2261 2259 sys.ps2 to prompt the next line."""
2262 2260
2263 2261 # We need to ensure that the source is unicode from here on.
2264 2262 if type(source)==str:
2265 2263 usource = source.decode(self.stdin_encoding)
2266 2264 else:
2267 2265 usource = source
2268 2266
2269 2267 if 0: # dbg
2270 2268 print 'Source:', repr(source) # dbg
2271 2269 print 'USource:', repr(usource) # dbg
2272 2270 print 'type:', type(source) # dbg
2273 2271 print 'encoding', self.stdin_encoding # dbg
2274 2272
2275 2273 try:
2276 2274 code = self.compile(usource, symbol, self.execution_count)
2277 2275 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2278 2276 # Case 1
2279 2277 self.showsyntaxerror(filename)
2280 2278 return None
2281 2279
2282 2280 if code is None:
2283 2281 # Case 2
2284 2282 return True
2285 2283
2286 2284 # Case 3
2287 2285 # We store the code object so that threaded shells and
2288 2286 # custom exception handlers can access all this info if needed.
2289 2287 # The source corresponding to this can be obtained from the
2290 2288 # buffer attribute as '\n'.join(self.buffer).
2291 2289 self.code_to_run = code
2292 2290 # now actually execute the code object
2293 2291 if self.run_code(code, post_execute) == 0:
2294 2292 return False
2295 2293 else:
2296 2294 return None
2297 2295
2298 2296 # For backwards compatibility
2299 2297 runsource = run_source
2300 2298
2301 2299 def run_code(self, code_obj, post_execute=True):
2302 2300 """Execute a code object.
2303 2301
2304 2302 When an exception occurs, self.showtraceback() is called to display a
2305 2303 traceback.
2306 2304
2307 2305 Return value: a flag indicating whether the code to be run completed
2308 2306 successfully:
2309 2307
2310 2308 - 0: successful execution.
2311 2309 - 1: an error occurred.
2312 2310 """
2313 2311
2314 2312 # Set our own excepthook in case the user code tries to call it
2315 2313 # directly, so that the IPython crash handler doesn't get triggered
2316 2314 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2317 2315
2318 2316 # we save the original sys.excepthook in the instance, in case config
2319 2317 # code (such as magics) needs access to it.
2320 2318 self.sys_excepthook = old_excepthook
2321 2319 outflag = 1 # happens in more places, so it's easier as default
2322 2320 try:
2323 2321 try:
2324 2322 self.hooks.pre_run_code_hook()
2325 2323 #rprint('Running code', repr(code_obj)) # dbg
2326 2324 exec code_obj in self.user_global_ns, self.user_ns
2327 2325 finally:
2328 2326 # Reset our crash handler in place
2329 2327 sys.excepthook = old_excepthook
2330 2328 except SystemExit:
2331 2329 self.reset_buffer()
2332 2330 self.showtraceback(exception_only=True)
2333 2331 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2334 2332 except self.custom_exceptions:
2335 2333 etype,value,tb = sys.exc_info()
2336 2334 self.CustomTB(etype,value,tb)
2337 2335 except:
2338 2336 self.showtraceback()
2339 2337 else:
2340 2338 outflag = 0
2341 2339 if softspace(sys.stdout, 0):
2342 2340 print
2343 2341
2344 2342 # Execute any registered post-execution functions. Here, any errors
2345 2343 # are reported only minimally and just on the terminal, because the
2346 2344 # main exception channel may be occupied with a user traceback.
2347 2345 # FIXME: we need to think this mechanism a little more carefully.
2348 2346 if post_execute:
2349 2347 for func in self._post_execute:
2350 2348 try:
2351 2349 func()
2352 2350 except:
2353 2351 head = '[ ERROR ] Evaluating post_execute function: %s' % \
2354 2352 func
2355 2353 print >> io.Term.cout, head
2356 2354 print >> io.Term.cout, self._simple_error()
2357 2355 print >> io.Term.cout, 'Removing from post_execute'
2358 2356 self._post_execute.remove(func)
2359 2357
2360 2358 # Flush out code object which has been run (and source)
2361 2359 self.code_to_run = None
2362 2360 return outflag
2363 2361
2364 2362 # For backwards compatibility
2365 2363 runcode = run_code
2366 2364
2367 2365 # PENDING REMOVAL: this method is slated for deletion, once our new
2368 2366 # input logic has been 100% moved to frontends and is stable.
2369 2367 def push_line(self, line):
2370 2368 """Push a line to the interpreter.
2371 2369
2372 2370 The line should not have a trailing newline; it may have
2373 2371 internal newlines. The line is appended to a buffer and the
2374 2372 interpreter's run_source() method is called with the
2375 2373 concatenated contents of the buffer as source. If this
2376 2374 indicates that the command was executed or invalid, the buffer
2377 2375 is reset; otherwise, the command is incomplete, and the buffer
2378 2376 is left as it was after the line was appended. The return
2379 2377 value is 1 if more input is required, 0 if the line was dealt
2380 2378 with in some way (this is the same as run_source()).
2381 2379 """
2382 2380
2383 2381 # autoindent management should be done here, and not in the
2384 2382 # interactive loop, since that one is only seen by keyboard input. We
2385 2383 # need this done correctly even for code run via runlines (which uses
2386 2384 # push).
2387 2385
2388 2386 #print 'push line: <%s>' % line # dbg
2389 2387 self.buffer.append(line)
2390 2388 full_source = '\n'.join(self.buffer)
2391 2389 more = self.run_source(full_source, self.filename)
2392 2390 if not more:
2393 2391 self.history_manager.store_inputs(self.execution_count,
2394 2392 '\n'.join(self.buffer_raw), full_source)
2395 2393 self.reset_buffer()
2396 2394 self.execution_count += 1
2397 2395 return more
2398 2396
2399 2397 def reset_buffer(self):
2400 2398 """Reset the input buffer."""
2401 2399 self.buffer[:] = []
2402 2400 self.buffer_raw[:] = []
2403 2401 self.input_splitter.reset()
2404 2402
2405 2403 # For backwards compatibility
2406 2404 resetbuffer = reset_buffer
2407 2405
2408 2406 def _is_secondary_block_start(self, s):
2409 2407 if not s.endswith(':'):
2410 2408 return False
2411 2409 if (s.startswith('elif') or
2412 2410 s.startswith('else') or
2413 2411 s.startswith('except') or
2414 2412 s.startswith('finally')):
2415 2413 return True
2416 2414
2417 2415 def _cleanup_ipy_script(self, script):
2418 2416 """Make a script safe for self.runlines()
2419 2417
2420 2418 Currently, IPython is lines based, with blocks being detected by
2421 2419 empty lines. This is a problem for block based scripts that may
2422 2420 not have empty lines after blocks. This script adds those empty
2423 2421 lines to make scripts safe for running in the current line based
2424 2422 IPython.
2425 2423 """
2426 2424 res = []
2427 2425 lines = script.splitlines()
2428 2426 level = 0
2429 2427
2430 2428 for l in lines:
2431 2429 lstripped = l.lstrip()
2432 2430 stripped = l.strip()
2433 2431 if not stripped:
2434 2432 continue
2435 2433 newlevel = len(l) - len(lstripped)
2436 2434 if level > 0 and newlevel == 0 and \
2437 2435 not self._is_secondary_block_start(stripped):
2438 2436 # add empty line
2439 2437 res.append('')
2440 2438 res.append(l)
2441 2439 level = newlevel
2442 2440
2443 2441 return '\n'.join(res) + '\n'
2444 2442
2445 2443 #-------------------------------------------------------------------------
2446 2444 # Things related to GUI support and pylab
2447 2445 #-------------------------------------------------------------------------
2448 2446
2449 2447 def enable_pylab(self, gui=None):
2450 2448 raise NotImplementedError('Implement enable_pylab in a subclass')
2451 2449
2452 2450 #-------------------------------------------------------------------------
2453 2451 # Utilities
2454 2452 #-------------------------------------------------------------------------
2455 2453
2456 2454 def var_expand(self,cmd,depth=0):
2457 2455 """Expand python variables in a string.
2458 2456
2459 2457 The depth argument indicates how many frames above the caller should
2460 2458 be walked to look for the local namespace where to expand variables.
2461 2459
2462 2460 The global namespace for expansion is always the user's interactive
2463 2461 namespace.
2464 2462 """
2465 2463
2466 2464 return str(ItplNS(cmd,
2467 2465 self.user_ns, # globals
2468 2466 # Skip our own frame in searching for locals:
2469 2467 sys._getframe(depth+1).f_locals # locals
2470 2468 ))
2471 2469
2472 2470 def mktempfile(self, data=None, prefix='ipython_edit_'):
2473 2471 """Make a new tempfile and return its filename.
2474 2472
2475 2473 This makes a call to tempfile.mktemp, but it registers the created
2476 2474 filename internally so ipython cleans it up at exit time.
2477 2475
2478 2476 Optional inputs:
2479 2477
2480 2478 - data(None): if data is given, it gets written out to the temp file
2481 2479 immediately, and the file is closed again."""
2482 2480
2483 2481 filename = tempfile.mktemp('.py', prefix)
2484 2482 self.tempfiles.append(filename)
2485 2483
2486 2484 if data:
2487 2485 tmp_file = open(filename,'w')
2488 2486 tmp_file.write(data)
2489 2487 tmp_file.close()
2490 2488 return filename
2491 2489
2492 2490 # TODO: This should be removed when Term is refactored.
2493 2491 def write(self,data):
2494 2492 """Write a string to the default output"""
2495 2493 io.Term.cout.write(data)
2496 2494
2497 2495 # TODO: This should be removed when Term is refactored.
2498 2496 def write_err(self,data):
2499 2497 """Write a string to the default error output"""
2500 2498 io.Term.cerr.write(data)
2501 2499
2502 2500 def ask_yes_no(self,prompt,default=True):
2503 2501 if self.quiet:
2504 2502 return True
2505 2503 return ask_yes_no(prompt,default)
2506 2504
2507 2505 def show_usage(self):
2508 2506 """Show a usage message"""
2509 2507 page.page(IPython.core.usage.interactive_usage)
2510 2508
2511 2509 #-------------------------------------------------------------------------
2512 2510 # Things related to IPython exiting
2513 2511 #-------------------------------------------------------------------------
2514 2512 def atexit_operations(self):
2515 2513 """This will be executed at the time of exit.
2516 2514
2517 2515 Cleanup operations and saving of persistent data that is done
2518 2516 unconditionally by IPython should be performed here.
2519 2517
2520 2518 For things that may depend on startup flags or platform specifics (such
2521 2519 as having readline or not), register a separate atexit function in the
2522 2520 code that has the appropriate information, rather than trying to
2523 2521 clutter
2524 2522 """
2525 2523 # Cleanup all tempfiles left around
2526 2524 for tfile in self.tempfiles:
2527 2525 try:
2528 2526 os.unlink(tfile)
2529 2527 except OSError:
2530 2528 pass
2531 2529
2532 2530 # Write anything in the history cache to the database.
2533 2531 self.history_manager.writeout_cache()
2534 2532
2535 2533 # Clear all user namespaces to release all references cleanly.
2536 self.reset()
2534 self.reset(new_session=False)
2537 2535
2538 2536 # Run user hooks
2539 2537 self.hooks.shutdown_hook()
2540 2538
2541 2539 def cleanup(self):
2542 2540 self.restore_sys_module_state()
2543 2541
2544 2542
2545 2543 class InteractiveShellABC(object):
2546 2544 """An abstract base class for InteractiveShell."""
2547 2545 __metaclass__ = abc.ABCMeta
2548 2546
2549 2547 InteractiveShellABC.register(InteractiveShell)
@@ -1,398 +1,397 b''
1 1 """Tests for various magic functions.
2 2
3 3 Needs to be run by nose (to make ipython session available).
4 4 """
5 5 from __future__ import absolute_import
6 6
7 7 #-----------------------------------------------------------------------------
8 8 # Imports
9 9 #-----------------------------------------------------------------------------
10 10
11 11 import os
12 12 import sys
13 13 import tempfile
14 14 import types
15 15 from cStringIO import StringIO
16 16
17 17 import nose.tools as nt
18 18
19 19 from IPython.utils.path import get_long_path_name
20 20 from IPython.testing import decorators as dec
21 21 from IPython.testing import tools as tt
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Test functions begin
25 25 #-----------------------------------------------------------------------------
26 26 def test_rehashx():
27 27 # clear up everything
28 28 _ip = get_ipython()
29 29 _ip.alias_manager.alias_table.clear()
30 30 del _ip.db['syscmdlist']
31 31
32 32 _ip.magic('rehashx')
33 33 # Practically ALL ipython development systems will have more than 10 aliases
34 34
35 35 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
36 36 for key, val in _ip.alias_manager.alias_table.iteritems():
37 37 # we must strip dots from alias names
38 38 nt.assert_true('.' not in key)
39 39
40 40 # rehashx must fill up syscmdlist
41 41 scoms = _ip.db['syscmdlist']
42 42 yield (nt.assert_true, len(scoms) > 10)
43 43
44 44
45 45 def test_magic_parse_options():
46 46 """Test that we don't mangle paths when parsing magic options."""
47 47 ip = get_ipython()
48 48 path = 'c:\\x'
49 49 opts = ip.parse_options('-f %s' % path,'f:')[0]
50 50 # argv splitting is os-dependent
51 51 if os.name == 'posix':
52 52 expected = 'c:x'
53 53 else:
54 54 expected = path
55 55 nt.assert_equals(opts['f'], expected)
56 56
57 57
58 58 def doctest_hist_f():
59 59 """Test %hist -f with temporary filename.
60 60
61 61 In [9]: import tempfile
62 62
63 63 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
64 64
65 65 In [11]: %hist -nl -f $tfile 3
66 66
67 67 In [13]: import os; os.unlink(tfile)
68 68 """
69 69
70 70
71 71 def doctest_hist_r():
72 72 """Test %hist -r
73 73
74 74 XXX - This test is not recording the output correctly. For some reason, in
75 75 testing mode the raw history isn't getting populated. No idea why.
76 76 Disabling the output checking for now, though at least we do run it.
77 77
78 78 In [1]: 'hist' in _ip.lsmagic()
79 79 Out[1]: True
80 80
81 81 In [2]: x=1
82 82
83 83 In [3]: %hist -rl 2
84 84 x=1 # random
85 85 %hist -r 2
86 86 """
87 87
88 88 def doctest_hist_op():
89 89 """Test %hist -op
90 90
91 91 In [1]: class b:
92 92 ...: pass
93 93 ...:
94 94
95 95 In [2]: class s(b):
96 96 ...: def __str__(self):
97 97 ...: return 's'
98 98 ...:
99 99
100 100 In [3]:
101 101
102 102 In [4]: class r(b):
103 103 ...: def __repr__(self):
104 104 ...: return 'r'
105 105 ...:
106 106
107 107 In [5]: class sr(s,r): pass
108 108 ...:
109 109
110 110 In [6]:
111 111
112 112 In [7]: bb=b()
113 113
114 114 In [8]: ss=s()
115 115
116 116 In [9]: rr=r()
117 117
118 118 In [10]: ssrr=sr()
119 119
120 120 In [11]: bb
121 121 Out[11]: <...b instance at ...>
122 122
123 123 In [12]: ss
124 124 Out[12]: <...s instance at ...>
125 125
126 126 In [13]:
127 127
128 128 In [14]: %hist -op
129 129 >>> class b:
130 130 ... pass
131 131 ...
132 132 >>> class s(b):
133 133 ... def __str__(self):
134 134 ... return 's'
135 135 ...
136 136 >>>
137 137 >>> class r(b):
138 138 ... def __repr__(self):
139 139 ... return 'r'
140 140 ...
141 141 >>> class sr(s,r): pass
142 142 >>>
143 143 >>> bb=b()
144 144 >>> ss=s()
145 145 >>> rr=r()
146 146 >>> ssrr=sr()
147 147 >>> bb
148 148 <...b instance at ...>
149 149 >>> ss
150 150 <...s instance at ...>
151 151 >>>
152 152 """
153 153
154 154 def test_macro():
155 155 ip = get_ipython()
156 156 ip.history_manager.reset() # Clear any existing history.
157 157 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
158 158 for i, cmd in enumerate(cmds, start=1):
159 159 ip.history_manager.store_inputs(i, cmd)
160 print i, cmd
161 160 ip.magic("macro test 1-3")
162 161 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
163 162
164 163 # List macros.
165 164 assert "test" in ip.magic("macro")
166 165
167 166
168 167 # XXX failing for now, until we get clearcmd out of quarantine. But we should
169 168 # fix this and revert the skip to happen only if numpy is not around.
170 169 #@dec.skipif_not_numpy
171 170 @dec.skip_known_failure
172 171 def test_numpy_clear_array_undec():
173 172 from IPython.extensions import clearcmd
174 173
175 174 _ip.ex('import numpy as np')
176 175 _ip.ex('a = np.empty(2)')
177 176 yield (nt.assert_true, 'a' in _ip.user_ns)
178 177 _ip.magic('clear array')
179 178 yield (nt.assert_false, 'a' in _ip.user_ns)
180 179
181 180
182 181 # Multiple tests for clipboard pasting
183 182 @dec.parametric
184 183 def test_paste():
185 184 _ip = get_ipython()
186 185 def paste(txt, flags='-q'):
187 186 """Paste input text, by default in quiet mode"""
188 187 hooks.clipboard_get = lambda : txt
189 188 _ip.magic('paste '+flags)
190 189
191 190 # Inject fake clipboard hook but save original so we can restore it later
192 191 hooks = _ip.hooks
193 192 user_ns = _ip.user_ns
194 193 original_clip = hooks.clipboard_get
195 194
196 195 try:
197 196 # This try/except with an emtpy except clause is here only because
198 197 # try/yield/finally is invalid syntax in Python 2.4. This will be
199 198 # removed when we drop 2.4-compatibility, and the emtpy except below
200 199 # will be changed to a finally.
201 200
202 201 # Run tests with fake clipboard function
203 202 user_ns.pop('x', None)
204 203 paste('x=1')
205 204 yield nt.assert_equal(user_ns['x'], 1)
206 205
207 206 user_ns.pop('x', None)
208 207 paste('>>> x=2')
209 208 yield nt.assert_equal(user_ns['x'], 2)
210 209
211 210 paste("""
212 211 >>> x = [1,2,3]
213 212 >>> y = []
214 213 >>> for i in x:
215 214 ... y.append(i**2)
216 215 ...
217 216 """)
218 217 yield nt.assert_equal(user_ns['x'], [1,2,3])
219 218 yield nt.assert_equal(user_ns['y'], [1,4,9])
220 219
221 220 # Now, test that paste -r works
222 221 user_ns.pop('x', None)
223 222 yield nt.assert_false('x' in user_ns)
224 223 _ip.magic('paste -r')
225 224 yield nt.assert_equal(user_ns['x'], [1,2,3])
226 225
227 226 # Also test paste echoing, by temporarily faking the writer
228 227 w = StringIO()
229 228 writer = _ip.write
230 229 _ip.write = w.write
231 230 code = """
232 231 a = 100
233 232 b = 200"""
234 233 try:
235 234 paste(code,'')
236 235 out = w.getvalue()
237 236 finally:
238 237 _ip.write = writer
239 238 yield nt.assert_equal(user_ns['a'], 100)
240 239 yield nt.assert_equal(user_ns['b'], 200)
241 240 yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
242 241
243 242 finally:
244 243 # This should be in a finally clause, instead of the bare except above.
245 244 # Restore original hook
246 245 hooks.clipboard_get = original_clip
247 246
248 247
249 248 def test_time():
250 249 _ip.magic('time None')
251 250
252 251
253 252 def doctest_time():
254 253 """
255 254 In [10]: %time None
256 255 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
257 256 Wall time: 0.00 s
258 257 """
259 258
260 259
261 260 def test_doctest_mode():
262 261 "Toggle doctest_mode twice, it should be a no-op and run without error"
263 262 _ip.magic('doctest_mode')
264 263 _ip.magic('doctest_mode')
265 264
266 265
267 266 def test_parse_options():
268 267 """Tests for basic options parsing in magics."""
269 268 # These are only the most minimal of tests, more should be added later. At
270 269 # the very least we check that basic text/unicode calls work OK.
271 270 nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo')
272 271 nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo')
273 272
274 273
275 274 def test_dirops():
276 275 """Test various directory handling operations."""
277 276 curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
278 277
279 278 startdir = os.getcwd()
280 279 ipdir = _ip.ipython_dir
281 280 try:
282 281 _ip.magic('cd "%s"' % ipdir)
283 282 nt.assert_equal(curpath(), ipdir)
284 283 _ip.magic('cd -')
285 284 nt.assert_equal(curpath(), startdir)
286 285 _ip.magic('pushd "%s"' % ipdir)
287 286 nt.assert_equal(curpath(), ipdir)
288 287 _ip.magic('popd')
289 288 nt.assert_equal(curpath(), startdir)
290 289 finally:
291 290 os.chdir(startdir)
292 291
293 292
294 293 def check_cpaste(code, should_fail=False):
295 294 """Execute code via 'cpaste' and ensure it was executed, unless
296 295 should_fail is set.
297 296 """
298 297 _ip.user_ns['code_ran'] = False
299 298
300 299 src = StringIO()
301 300 src.write('\n')
302 301 src.write(code)
303 302 src.write('\n--\n')
304 303 src.seek(0)
305 304
306 305 stdin_save = sys.stdin
307 306 sys.stdin = src
308 307
309 308 try:
310 309 _ip.magic('cpaste')
311 310 except:
312 311 if not should_fail:
313 312 raise AssertionError("Failure not expected : '%s'" %
314 313 code)
315 314 else:
316 315 assert _ip.user_ns['code_ran']
317 316 if should_fail:
318 317 raise AssertionError("Failure expected : '%s'" % code)
319 318 finally:
320 319 sys.stdin = stdin_save
321 320
322 321
323 322 def test_cpaste():
324 323 """Test cpaste magic"""
325 324
326 325 def run():
327 326 """Marker function: sets a flag when executed.
328 327 """
329 328 _ip.user_ns['code_ran'] = True
330 329 return 'run' # return string so '+ run()' doesn't result in success
331 330
332 331 tests = {'pass': ["> > > run()",
333 332 ">>> > run()",
334 333 "+++ run()",
335 334 "++ run()",
336 335 " >>> run()"],
337 336
338 337 'fail': ["+ + run()",
339 338 " ++ run()"]}
340 339
341 340 _ip.user_ns['run'] = run
342 341
343 342 for code in tests['pass']:
344 343 check_cpaste(code)
345 344
346 345 for code in tests['fail']:
347 346 check_cpaste(code, should_fail=True)
348 347
349 348 def test_xmode():
350 349 # Calling xmode three times should be a no-op
351 350 xmode = _ip.InteractiveTB.mode
352 351 for i in range(3):
353 352 _ip.magic("xmode")
354 353 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
355 354
356 355 def doctest_who():
357 356 """doctest for %who
358 357
359 358 In [1]: %reset -f
360 359
361 360 In [2]: alpha = 123
362 361
363 362 In [3]: beta = 'beta'
364 363
365 364 In [4]: %who int
366 365 alpha
367 366
368 367 In [5]: %who str
369 368 beta
370 369
371 370 In [6]: %whos
372 371 Variable Type Data/Info
373 372 ----------------------------
374 373 alpha int 123
375 374 beta str beta
376 375
377 376 In [7]: %who_ls
378 377 Out[7]: ['alpha', 'beta']
379 378 """
380 379
381 380 def doctest_precision():
382 381 """doctest for %precision
383 382
384 383 In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
385 384
386 385 In [2]: %precision 5
387 386 Out[2]: '%.5f'
388 387
389 388 In [3]: f.float_format
390 389 Out[3]: '%.5f'
391 390
392 391 In [4]: %precision %e
393 392 Out[4]: '%e'
394 393
395 394 In [5]: f(3.1415927)
396 395 Out[5]: '3.141593e+00'
397 396 """
398 397
@@ -1,494 +1,490 b''
1 1 """ A FrontendWidget that emulates the interface of the console IPython and
2 2 supports the additional functionality provided by the IPython kernel.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Imports
7 7 #-----------------------------------------------------------------------------
8 8
9 9 # Standard library imports
10 10 from collections import namedtuple
11 11 import re
12 12 from subprocess import Popen
13 13 from textwrap import dedent
14 14
15 15 # System library imports
16 16 from IPython.external.qt import QtCore, QtGui
17 17
18 18 # Local imports
19 19 from IPython.core.inputsplitter import IPythonInputSplitter, \
20 20 transform_ipy_prompt
21 21 from IPython.core.usage import default_gui_banner
22 22 from IPython.utils.traitlets import Bool, Str
23 23 from frontend_widget import FrontendWidget
24 24 from styles import (default_light_style_sheet, default_light_syntax_style,
25 25 default_dark_style_sheet, default_dark_syntax_style,
26 26 default_bw_style_sheet, default_bw_syntax_style)
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Constants
30 30 #-----------------------------------------------------------------------------
31 31
32 32 # Default strings to build and display input and output prompts (and separators
33 33 # in between)
34 34 default_in_prompt = 'In [<span class="in-prompt-number">%i</span>]: '
35 35 default_out_prompt = 'Out[<span class="out-prompt-number">%i</span>]: '
36 36 default_input_sep = '\n'
37 37 default_output_sep = ''
38 38 default_output_sep2 = ''
39 39
40 40 # Base path for most payload sources.
41 41 zmq_shell_source = 'IPython.zmq.zmqshell.ZMQInteractiveShell'
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # IPythonWidget class
45 45 #-----------------------------------------------------------------------------
46 46
47 47 class IPythonWidget(FrontendWidget):
48 48 """ A FrontendWidget for an IPython kernel.
49 49 """
50 50
51 51 # If set, the 'custom_edit_requested(str, int)' signal will be emitted when
52 52 # an editor is needed for a file. This overrides 'editor' and 'editor_line'
53 53 # settings.
54 54 custom_edit = Bool(False)
55 55 custom_edit_requested = QtCore.Signal(object, object)
56 56
57 57 # A command for invoking a system text editor. If the string contains a
58 58 # {filename} format specifier, it will be used. Otherwise, the filename will
59 59 # be appended to the end the command.
60 60 editor = Str('default', config=True)
61 61
62 62 # The editor command to use when a specific line number is requested. The
63 63 # string should contain two format specifiers: {line} and {filename}. If
64 64 # this parameter is not specified, the line number option to the %edit magic
65 65 # will be ignored.
66 66 editor_line = Str(config=True)
67 67
68 68 # A CSS stylesheet. The stylesheet can contain classes for:
69 69 # 1. Qt: QPlainTextEdit, QFrame, QWidget, etc
70 70 # 2. Pygments: .c, .k, .o, etc (see PygmentsHighlighter)
71 71 # 3. IPython: .error, .in-prompt, .out-prompt, etc
72 72 style_sheet = Str(config=True)
73 73
74 74 # If not empty, use this Pygments style for syntax highlighting. Otherwise,
75 75 # the style sheet is queried for Pygments style information.
76 76 syntax_style = Str(config=True)
77 77
78 78 # Prompts.
79 79 in_prompt = Str(default_in_prompt, config=True)
80 80 out_prompt = Str(default_out_prompt, config=True)
81 81 input_sep = Str(default_input_sep, config=True)
82 82 output_sep = Str(default_output_sep, config=True)
83 83 output_sep2 = Str(default_output_sep2, config=True)
84 84
85 85 # FrontendWidget protected class variables.
86 86 _input_splitter_class = IPythonInputSplitter
87 87
88 88 # IPythonWidget protected class variables.
89 89 _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number'])
90 90 _payload_source_edit = zmq_shell_source + '.edit_magic'
91 91 _payload_source_exit = zmq_shell_source + '.ask_exit'
92 92 _payload_source_loadpy = zmq_shell_source + '.magic_loadpy'
93 93 _payload_source_page = 'IPython.zmq.page.page'
94 94
95 95 #---------------------------------------------------------------------------
96 96 # 'object' interface
97 97 #---------------------------------------------------------------------------
98 98
99 99 def __init__(self, *args, **kw):
100 100 super(IPythonWidget, self).__init__(*args, **kw)
101 101
102 102 # IPythonWidget protected variables.
103 103 self._code_to_load = None
104 104 self._payload_handlers = {
105 105 self._payload_source_edit : self._handle_payload_edit,
106 106 self._payload_source_exit : self._handle_payload_exit,
107 107 self._payload_source_page : self._handle_payload_page,
108 108 self._payload_source_loadpy : self._handle_payload_loadpy }
109 109 self._previous_prompt_obj = None
110 110 self._keep_kernel_on_exit = None
111 111
112 112 # Initialize widget styling.
113 113 if self.style_sheet:
114 114 self._style_sheet_changed()
115 115 self._syntax_style_changed()
116 116 else:
117 117 self.set_default_style()
118 118
119 119 #---------------------------------------------------------------------------
120 120 # 'BaseFrontendMixin' abstract interface
121 121 #---------------------------------------------------------------------------
122 122
123 123 def _handle_complete_reply(self, rep):
124 124 """ Reimplemented to support IPython's improved completion machinery.
125 125 """
126 126 cursor = self._get_cursor()
127 127 info = self._request_info.get('complete')
128 128 if info and info.id == rep['parent_header']['msg_id'] and \
129 129 info.pos == cursor.position():
130 130 matches = rep['content']['matches']
131 131 text = rep['content']['matched_text']
132 132 offset = len(text)
133 133
134 134 # Clean up matches with period and path separators if the matched
135 135 # text has not been transformed. This is done by truncating all
136 136 # but the last component and then suitably decreasing the offset
137 137 # between the current cursor position and the start of completion.
138 138 if len(matches) > 1 and matches[0][:offset] == text:
139 139 parts = re.split(r'[./\\]', text)
140 140 sep_count = len(parts) - 1
141 141 if sep_count:
142 142 chop_length = sum(map(len, parts[:sep_count])) + sep_count
143 143 matches = [ match[chop_length:] for match in matches ]
144 144 offset -= chop_length
145 145
146 146 # Move the cursor to the start of the match and complete.
147 147 cursor.movePosition(QtGui.QTextCursor.Left, n=offset)
148 148 self._complete_with_items(cursor, matches)
149 149
150 150 def _handle_execute_reply(self, msg):
151 151 """ Reimplemented to support prompt requests.
152 152 """
153 153 info = self._request_info.get('execute')
154 154 if info and info.id == msg['parent_header']['msg_id']:
155 155 if info.kind == 'prompt':
156 156 number = msg['content']['execution_count'] + 1
157 157 self._show_interpreter_prompt(number)
158 158 else:
159 159 super(IPythonWidget, self)._handle_execute_reply(msg)
160 160
161 def _handle_history_reply(self, msg):
162 """ Implemented to handle history replies, which are only supported by
163 the IPython kernel.
161 def _handle_history_tail_reply(self, msg):
162 """ Implemented to handle history tail replies, which are only supported
163 by the IPython kernel.
164 164 """
165 history_dict = msg['content']['history']
166 input_history_dict = {}
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()) ]
165 history_items = msg['content']['history']
166 items = [ line.rstrip() for _, _, line in history_items ]
170 167 self._set_history(items)
171 168
172 169 def _handle_pyout(self, msg):
173 170 """ Reimplemented for IPython-style "display hook".
174 171 """
175 172 if not self._hidden and self._is_from_this_session(msg):
176 173 content = msg['content']
177 174 prompt_number = content['execution_count']
178 175 data = content['data']
179 176 if data.has_key('text/html'):
180 177 self._append_plain_text(self.output_sep)
181 178 self._append_html(self._make_out_prompt(prompt_number))
182 179 html = data['text/html']
183 180 self._append_plain_text('\n')
184 181 self._append_html(html + self.output_sep2)
185 182 elif data.has_key('text/plain'):
186 183 self._append_plain_text(self.output_sep)
187 184 self._append_html(self._make_out_prompt(prompt_number))
188 185 text = data['text/plain']
189 186 self._append_plain_text(text + self.output_sep2)
190 187
191 188 def _handle_display_data(self, msg):
192 189 """ The base handler for the ``display_data`` message.
193 190 """
194 191 # For now, we don't display data from other frontends, but we
195 192 # eventually will as this allows all frontends to monitor the display
196 193 # data. But we need to figure out how to handle this in the GUI.
197 194 if not self._hidden and self._is_from_this_session(msg):
198 195 source = msg['content']['source']
199 196 data = msg['content']['data']
200 197 metadata = msg['content']['metadata']
201 198 # In the regular IPythonWidget, we simply print the plain text
202 199 # representation.
203 200 if data.has_key('text/html'):
204 201 html = data['text/html']
205 202 self._append_html(html)
206 203 elif data.has_key('text/plain'):
207 204 text = data['text/plain']
208 205 self._append_plain_text(text)
209 206 # This newline seems to be needed for text and html output.
210 207 self._append_plain_text(u'\n')
211 208
212 209 def _started_channels(self):
213 210 """ Reimplemented to make a history request.
214 211 """
215 212 super(IPythonWidget, self)._started_channels()
216 self.kernel_manager.xreq_channel.history(raw=True, output=False,
217 this_session=False)
213 self.kernel_manager.xreq_channel.history_tail(1000)
218 214
219 215 #---------------------------------------------------------------------------
220 216 # 'ConsoleWidget' public interface
221 217 #---------------------------------------------------------------------------
222 218
223 219 def copy(self):
224 220 """ Copy the currently selected text to the clipboard, removing prompts
225 221 if possible.
226 222 """
227 223 text = self._control.textCursor().selection().toPlainText()
228 224 if text:
229 225 lines = map(transform_ipy_prompt, text.splitlines())
230 226 text = '\n'.join(lines)
231 227 QtGui.QApplication.clipboard().setText(text)
232 228
233 229 #---------------------------------------------------------------------------
234 230 # 'FrontendWidget' public interface
235 231 #---------------------------------------------------------------------------
236 232
237 233 def execute_file(self, path, hidden=False):
238 234 """ Reimplemented to use the 'run' magic.
239 235 """
240 236 self.execute('%%run %s' % path, hidden=hidden)
241 237
242 238 #---------------------------------------------------------------------------
243 239 # 'FrontendWidget' protected interface
244 240 #---------------------------------------------------------------------------
245 241
246 242 def _complete(self):
247 243 """ Reimplemented to support IPython's improved completion machinery.
248 244 """
249 245 # We let the kernel split the input line, so we *always* send an empty
250 246 # text field. Readline-based frontends do get a real text field which
251 247 # they can use.
252 248 text = ''
253 249
254 250 # Send the completion request to the kernel
255 251 msg_id = self.kernel_manager.xreq_channel.complete(
256 252 text, # text
257 253 self._get_input_buffer_cursor_line(), # line
258 254 self._get_input_buffer_cursor_column(), # cursor_pos
259 255 self.input_buffer) # block
260 256 pos = self._get_cursor().position()
261 257 info = self._CompletionRequest(msg_id, pos)
262 258 self._request_info['complete'] = info
263 259
264 260 def _get_banner(self):
265 261 """ Reimplemented to return IPython's default banner.
266 262 """
267 263 return default_gui_banner
268 264
269 265 def _process_execute_error(self, msg):
270 266 """ Reimplemented for IPython-style traceback formatting.
271 267 """
272 268 content = msg['content']
273 269 traceback = '\n'.join(content['traceback']) + '\n'
274 270 if False:
275 271 # FIXME: For now, tracebacks come as plain text, so we can't use
276 272 # the html renderer yet. Once we refactor ultratb to produce
277 273 # properly styled tracebacks, this branch should be the default
278 274 traceback = traceback.replace(' ', '&nbsp;')
279 275 traceback = traceback.replace('\n', '<br/>')
280 276
281 277 ename = content['ename']
282 278 ename_styled = '<span class="error">%s</span>' % ename
283 279 traceback = traceback.replace(ename, ename_styled)
284 280
285 281 self._append_html(traceback)
286 282 else:
287 283 # This is the fallback for now, using plain text with ansi escapes
288 284 self._append_plain_text(traceback)
289 285
290 286 def _process_execute_payload(self, item):
291 287 """ Reimplemented to dispatch payloads to handler methods.
292 288 """
293 289 handler = self._payload_handlers.get(item['source'])
294 290 if handler is None:
295 291 # We have no handler for this type of payload, simply ignore it
296 292 return False
297 293 else:
298 294 handler(item)
299 295 return True
300 296
301 297 def _show_interpreter_prompt(self, number=None):
302 298 """ Reimplemented for IPython-style prompts.
303 299 """
304 300 # If a number was not specified, make a prompt number request.
305 301 if number is None:
306 302 msg_id = self.kernel_manager.xreq_channel.execute('', silent=True)
307 303 info = self._ExecutionRequest(msg_id, 'prompt')
308 304 self._request_info['execute'] = info
309 305 return
310 306
311 307 # Show a new prompt and save information about it so that it can be
312 308 # updated later if the prompt number turns out to be wrong.
313 309 self._prompt_sep = self.input_sep
314 310 self._show_prompt(self._make_in_prompt(number), html=True)
315 311 block = self._control.document().lastBlock()
316 312 length = len(self._prompt)
317 313 self._previous_prompt_obj = self._PromptBlock(block, length, number)
318 314
319 315 # Update continuation prompt to reflect (possibly) new prompt length.
320 316 self._set_continuation_prompt(
321 317 self._make_continuation_prompt(self._prompt), html=True)
322 318
323 319 # Load code from the %loadpy magic, if necessary.
324 320 if self._code_to_load is not None:
325 321 self.input_buffer = dedent(self._code_to_load.rstrip())
326 322 self._code_to_load = None
327 323
328 324 def _show_interpreter_prompt_for_reply(self, msg):
329 325 """ Reimplemented for IPython-style prompts.
330 326 """
331 327 # Update the old prompt number if necessary.
332 328 content = msg['content']
333 329 previous_prompt_number = content['execution_count']
334 330 if self._previous_prompt_obj and \
335 331 self._previous_prompt_obj.number != previous_prompt_number:
336 332 block = self._previous_prompt_obj.block
337 333
338 334 # Make sure the prompt block has not been erased.
339 335 if block.isValid() and block.text():
340 336
341 337 # Remove the old prompt and insert a new prompt.
342 338 cursor = QtGui.QTextCursor(block)
343 339 cursor.movePosition(QtGui.QTextCursor.Right,
344 340 QtGui.QTextCursor.KeepAnchor,
345 341 self._previous_prompt_obj.length)
346 342 prompt = self._make_in_prompt(previous_prompt_number)
347 343 self._prompt = self._insert_html_fetching_plain_text(
348 344 cursor, prompt)
349 345
350 346 # When the HTML is inserted, Qt blows away the syntax
351 347 # highlighting for the line, so we need to rehighlight it.
352 348 self._highlighter.rehighlightBlock(cursor.block())
353 349
354 350 self._previous_prompt_obj = None
355 351
356 352 # Show a new prompt with the kernel's estimated prompt number.
357 353 self._show_interpreter_prompt(previous_prompt_number + 1)
358 354
359 355 #---------------------------------------------------------------------------
360 356 # 'IPythonWidget' interface
361 357 #---------------------------------------------------------------------------
362 358
363 359 def set_default_style(self, colors='lightbg'):
364 360 """ Sets the widget style to the class defaults.
365 361
366 362 Parameters:
367 363 -----------
368 364 colors : str, optional (default lightbg)
369 365 Whether to use the default IPython light background or dark
370 366 background or B&W style.
371 367 """
372 368 colors = colors.lower()
373 369 if colors=='lightbg':
374 370 self.style_sheet = default_light_style_sheet
375 371 self.syntax_style = default_light_syntax_style
376 372 elif colors=='linux':
377 373 self.style_sheet = default_dark_style_sheet
378 374 self.syntax_style = default_dark_syntax_style
379 375 elif colors=='nocolor':
380 376 self.style_sheet = default_bw_style_sheet
381 377 self.syntax_style = default_bw_syntax_style
382 378 else:
383 379 raise KeyError("No such color scheme: %s"%colors)
384 380
385 381 #---------------------------------------------------------------------------
386 382 # 'IPythonWidget' protected interface
387 383 #---------------------------------------------------------------------------
388 384
389 385 def _edit(self, filename, line=None):
390 386 """ Opens a Python script for editing.
391 387
392 388 Parameters:
393 389 -----------
394 390 filename : str
395 391 A path to a local system file.
396 392
397 393 line : int, optional
398 394 A line of interest in the file.
399 395 """
400 396 if self.custom_edit:
401 397 self.custom_edit_requested.emit(filename, line)
402 398 elif self.editor == 'default':
403 399 self._append_plain_text('No default editor available.\n')
404 400 else:
405 401 try:
406 402 filename = '"%s"' % filename
407 403 if line and self.editor_line:
408 404 command = self.editor_line.format(filename=filename,
409 405 line=line)
410 406 else:
411 407 try:
412 408 command = self.editor.format()
413 409 except KeyError:
414 410 command = self.editor.format(filename=filename)
415 411 else:
416 412 command += ' ' + filename
417 413 except KeyError:
418 414 self._append_plain_text('Invalid editor command.\n')
419 415 else:
420 416 try:
421 417 Popen(command, shell=True)
422 418 except OSError:
423 419 msg = 'Opening editor with command "%s" failed.\n'
424 420 self._append_plain_text(msg % command)
425 421
426 422 def _make_in_prompt(self, number):
427 423 """ Given a prompt number, returns an HTML In prompt.
428 424 """
429 425 body = self.in_prompt % number
430 426 return '<span class="in-prompt">%s</span>' % body
431 427
432 428 def _make_continuation_prompt(self, prompt):
433 429 """ Given a plain text version of an In prompt, returns an HTML
434 430 continuation prompt.
435 431 """
436 432 end_chars = '...: '
437 433 space_count = len(prompt.lstrip('\n')) - len(end_chars)
438 434 body = '&nbsp;' * space_count + end_chars
439 435 return '<span class="in-prompt">%s</span>' % body
440 436
441 437 def _make_out_prompt(self, number):
442 438 """ Given a prompt number, returns an HTML Out prompt.
443 439 """
444 440 body = self.out_prompt % number
445 441 return '<span class="out-prompt">%s</span>' % body
446 442
447 443 #------ Payload handlers --------------------------------------------------
448 444
449 445 # Payload handlers with a generic interface: each takes the opaque payload
450 446 # dict, unpacks it and calls the underlying functions with the necessary
451 447 # arguments.
452 448
453 449 def _handle_payload_edit(self, item):
454 450 self._edit(item['filename'], item['line_number'])
455 451
456 452 def _handle_payload_exit(self, item):
457 453 self._keep_kernel_on_exit = item['keepkernel']
458 454 self.exit_requested.emit()
459 455
460 456 def _handle_payload_loadpy(self, item):
461 457 # Simple save the text of the .py file for later. The text is written
462 458 # to the buffer when _prompt_started_hook is called.
463 459 self._code_to_load = item['text']
464 460
465 461 def _handle_payload_page(self, item):
466 462 # Since the plain text widget supports only a very small subset of HTML
467 463 # and we have no control over the HTML source, we only page HTML
468 464 # payloads in the rich text widget.
469 465 if item['html'] and self.kind == 'rich':
470 466 self._page(item['html'], html=True)
471 467 else:
472 468 self._page(item['text'], html=False)
473 469
474 470 #------ Trait change handlers --------------------------------------------
475 471
476 472 def _style_sheet_changed(self):
477 473 """ Set the style sheets of the underlying widgets.
478 474 """
479 475 self.setStyleSheet(self.style_sheet)
480 476 self._control.document().setDefaultStyleSheet(self.style_sheet)
481 477 if self._page_control:
482 478 self._page_control.document().setDefaultStyleSheet(self.style_sheet)
483 479
484 480 bg_color = self._control.palette().window().color()
485 481 self._ansi_processor.set_background_color(bg_color)
486 482
487 483 def _syntax_style_changed(self):
488 484 """ Set the style for the syntax highlighter.
489 485 """
490 486 if self.syntax_style:
491 487 self._highlighter.set_style(self.syntax_style)
492 488 else:
493 489 self._highlighter.set_style_sheet(self.style_sheet)
494 490
@@ -1,640 +1,639 b''
1 1 #!/usr/bin/env python
2 2 """A simple interactive kernel that talks to a frontend over 0MQ.
3 3
4 4 Things to do:
5 5
6 6 * Implement `set_parent` logic. Right before doing exec, the Kernel should
7 7 call set_parent on all the PUB objects with the message about to be executed.
8 8 * Implement random port and security key logic.
9 9 * Implement control messages.
10 10 * Implement event loop and poll version.
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from __future__ import print_function
17 17
18 18 # Standard library imports.
19 19 import __builtin__
20 20 import atexit
21 21 import sys
22 22 import time
23 23 import traceback
24 24 import logging
25 25 # System library imports.
26 26 import zmq
27 27
28 28 # Local imports.
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.utils import io
31 31 from IPython.utils.jsonutil import json_clean
32 32 from IPython.lib import pylabtools
33 33 from IPython.utils.traitlets import Instance, Float
34 34 from entry_point import (base_launch_kernel, make_argument_parser, make_kernel,
35 35 start_kernel)
36 36 from iostream import OutStream
37 37 from session import Session, Message
38 38 from zmqshell import ZMQInteractiveShell
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Globals
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # Module-level logger
45 45 logger = logging.getLogger(__name__)
46 46
47 47 #-----------------------------------------------------------------------------
48 48 # Main kernel class
49 49 #-----------------------------------------------------------------------------
50 50
51 51 class Kernel(Configurable):
52 52
53 53 #---------------------------------------------------------------------------
54 54 # Kernel interface
55 55 #---------------------------------------------------------------------------
56 56
57 57 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
58 58 session = Instance(Session)
59 59 reply_socket = Instance('zmq.Socket')
60 60 pub_socket = Instance('zmq.Socket')
61 61 req_socket = Instance('zmq.Socket')
62 62
63 63 # Private interface
64 64
65 65 # Time to sleep after flushing the stdout/err buffers in each execute
66 66 # cycle. While this introduces a hard limit on the minimal latency of the
67 67 # execute cycle, it helps prevent output synchronization problems for
68 68 # clients.
69 69 # Units are in seconds. The minimum zmq latency on local host is probably
70 70 # ~150 microseconds, set this to 500us for now. We may need to increase it
71 71 # a little if it's not enough after more interactive testing.
72 72 _execute_sleep = Float(0.0005, config=True)
73 73
74 74 # Frequency of the kernel's event loop.
75 75 # Units are in seconds, kernel subclasses for GUI toolkits may need to
76 76 # adapt to milliseconds.
77 77 _poll_interval = Float(0.05, config=True)
78 78
79 79 # If the shutdown was requested over the network, we leave here the
80 80 # necessary reply message so it can be sent by our registered atexit
81 81 # handler. This ensures that the reply is only sent to clients truly at
82 82 # the end of our shutdown process (which happens after the underlying
83 83 # IPython shell's own shutdown).
84 84 _shutdown_message = None
85 85
86 86 # This is a dict of port number that the kernel is listening on. It is set
87 87 # by record_ports and used by connect_request.
88 88 _recorded_ports = None
89 89
90 90
91 91 def __init__(self, **kwargs):
92 92 super(Kernel, self).__init__(**kwargs)
93 93
94 94 # Before we even start up the shell, register *first* our exit handlers
95 95 # so they come before the shell's
96 96 atexit.register(self._at_shutdown)
97 97
98 98 # Initialize the InteractiveShell subclass
99 99 self.shell = ZMQInteractiveShell.instance()
100 100 self.shell.displayhook.session = self.session
101 101 self.shell.displayhook.pub_socket = self.pub_socket
102 102 self.shell.display_pub.session = self.session
103 103 self.shell.display_pub.pub_socket = self.pub_socket
104 104
105 105 # TMP - hack while developing
106 106 self.shell._reply_content = None
107 107
108 108 # Build dict of handlers for message types
109 109 msg_types = [ 'execute_request', 'complete_request',
110 'object_info_request', 'history_request',
110 'object_info_request', 'history_tail_request',
111 111 'connect_request', 'shutdown_request']
112 112 self.handlers = {}
113 113 for msg_type in msg_types:
114 114 self.handlers[msg_type] = getattr(self, msg_type)
115 115
116 116 def do_one_iteration(self):
117 117 """Do one iteration of the kernel's evaluation loop.
118 118 """
119 119 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
120 120 if msg is None:
121 121 return
122 122
123 123 # This assert will raise in versions of zeromq 2.0.7 and lesser.
124 124 # We now require 2.0.8 or above, so we can uncomment for safety.
125 125 # print(ident,msg, file=sys.__stdout__)
126 126 assert ident is not None, "Missing message part."
127 127
128 128 # Print some info about this message and leave a '--->' marker, so it's
129 129 # easier to trace visually the message chain when debugging. Each
130 130 # handler prints its message at the end.
131 131 # Eventually we'll move these from stdout to a logger.
132 132 logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***')
133 133 logger.debug(' Content: '+str(msg['content'])+'\n --->\n ')
134 134
135 135 # Find and call actual handler for message
136 136 handler = self.handlers.get(msg['msg_type'], None)
137 137 if handler is None:
138 138 logger.error("UNKNOWN MESSAGE TYPE:" +str(msg))
139 139 else:
140 140 handler(ident, msg)
141 141
142 142 # Check whether we should exit, in case the incoming message set the
143 143 # exit flag on
144 144 if self.shell.exit_now:
145 145 logger.debug('\nExiting IPython kernel...')
146 146 # We do a normal, clean exit, which allows any actions registered
147 147 # via atexit (such as history saving) to take place.
148 148 sys.exit(0)
149 149
150 150
151 151 def start(self):
152 152 """ Start the kernel main loop.
153 153 """
154 154 while True:
155 155 time.sleep(self._poll_interval)
156 156 self.do_one_iteration()
157 157
158 158 def record_ports(self, xrep_port, pub_port, req_port, hb_port):
159 159 """Record the ports that this kernel is using.
160 160
161 161 The creator of the Kernel instance must call this methods if they
162 162 want the :meth:`connect_request` method to return the port numbers.
163 163 """
164 164 self._recorded_ports = {
165 165 'xrep_port' : xrep_port,
166 166 'pub_port' : pub_port,
167 167 'req_port' : req_port,
168 168 'hb_port' : hb_port
169 169 }
170 170
171 171 #---------------------------------------------------------------------------
172 172 # Kernel request handlers
173 173 #---------------------------------------------------------------------------
174 174
175 175 def _publish_pyin(self, code, parent):
176 176 """Publish the code request on the pyin stream."""
177 177
178 178 pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent)
179 179
180 180 def execute_request(self, ident, parent):
181 181
182 182 status_msg = self.session.send(self.pub_socket,
183 183 u'status',
184 184 {u'execution_state':u'busy'},
185 185 parent=parent
186 186 )
187 187
188 188 try:
189 189 content = parent[u'content']
190 190 code = content[u'code']
191 191 silent = content[u'silent']
192 192 except:
193 193 logger.error("Got bad msg: ")
194 194 logger.error(str(Message(parent)))
195 195 return
196 196
197 197 shell = self.shell # we'll need this a lot here
198 198
199 199 # Replace raw_input. Note that is not sufficient to replace
200 200 # raw_input in the user namespace.
201 201 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
202 202 __builtin__.raw_input = raw_input
203 203
204 204 # Set the parent message of the display hook and out streams.
205 205 shell.displayhook.set_parent(parent)
206 206 shell.display_pub.set_parent(parent)
207 207 sys.stdout.set_parent(parent)
208 208 sys.stderr.set_parent(parent)
209 209
210 210 # Re-broadcast our input for the benefit of listening clients, and
211 211 # start computing output
212 212 if not silent:
213 213 self._publish_pyin(code, parent)
214 214
215 215 reply_content = {}
216 216 try:
217 217 if silent:
218 218 # run_code uses 'exec' mode, so no displayhook will fire, and it
219 219 # doesn't call logging or history manipulations. Print
220 220 # statements in that code will obviously still execute.
221 221 shell.run_code(code)
222 222 else:
223 223 # FIXME: the shell calls the exception handler itself.
224 224 shell._reply_content = None
225 225 shell.run_cell(code)
226 226 except:
227 227 status = u'error'
228 228 # FIXME: this code right now isn't being used yet by default,
229 229 # because the runlines() call above directly fires off exception
230 230 # reporting. This code, therefore, is only active in the scenario
231 231 # where runlines itself has an unhandled exception. We need to
232 232 # uniformize this, for all exception construction to come from a
233 233 # single location in the codbase.
234 234 etype, evalue, tb = sys.exc_info()
235 235 tb_list = traceback.format_exception(etype, evalue, tb)
236 236 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
237 237 else:
238 238 status = u'ok'
239 239
240 240 reply_content[u'status'] = status
241 241
242 242 # Return the execution counter so clients can display prompts
243 243 reply_content['execution_count'] = shell.execution_count -1
244 244
245 245 # FIXME - fish exception info out of shell, possibly left there by
246 246 # runlines. We'll need to clean up this logic later.
247 247 if shell._reply_content is not None:
248 248 reply_content.update(shell._reply_content)
249 249
250 250 # At this point, we can tell whether the main code execution succeeded
251 251 # or not. If it did, we proceed to evaluate user_variables/expressions
252 252 if reply_content['status'] == 'ok':
253 253 reply_content[u'user_variables'] = \
254 254 shell.user_variables(content[u'user_variables'])
255 255 reply_content[u'user_expressions'] = \
256 256 shell.user_expressions(content[u'user_expressions'])
257 257 else:
258 258 # If there was an error, don't even try to compute variables or
259 259 # expressions
260 260 reply_content[u'user_variables'] = {}
261 261 reply_content[u'user_expressions'] = {}
262 262
263 263 # Payloads should be retrieved regardless of outcome, so we can both
264 264 # recover partial output (that could have been generated early in a
265 265 # block, before an error) and clear the payload system always.
266 266 reply_content[u'payload'] = shell.payload_manager.read_payload()
267 267 # Be agressive about clearing the payload because we don't want
268 268 # it to sit in memory until the next execute_request comes in.
269 269 shell.payload_manager.clear_payload()
270 270
271 271 # Flush output before sending the reply.
272 272 sys.stdout.flush()
273 273 sys.stderr.flush()
274 274 # FIXME: on rare occasions, the flush doesn't seem to make it to the
275 275 # clients... This seems to mitigate the problem, but we definitely need
276 276 # to better understand what's going on.
277 277 if self._execute_sleep:
278 278 time.sleep(self._execute_sleep)
279 279
280 280 # Send the reply.
281 281 reply_msg = self.session.send(self.reply_socket, u'execute_reply',
282 282 reply_content, parent, ident=ident)
283 283 logger.debug(str(reply_msg))
284 284
285 285 if reply_msg['content']['status'] == u'error':
286 286 self._abort_queue()
287 287
288 288 status_msg = self.session.send(self.pub_socket,
289 289 u'status',
290 290 {u'execution_state':u'idle'},
291 291 parent=parent
292 292 )
293 293
294 294 def complete_request(self, ident, parent):
295 295 txt, matches = self._complete(parent)
296 296 matches = {'matches' : matches,
297 297 'matched_text' : txt,
298 298 'status' : 'ok'}
299 299 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
300 300 matches, parent, ident)
301 301 logger.debug(str(completion_msg))
302 302
303 303 def object_info_request(self, ident, parent):
304 304 object_info = self.shell.object_inspect(parent['content']['oname'])
305 305 # Before we send this object over, we scrub it for JSON usage
306 306 oinfo = json_clean(object_info)
307 307 msg = self.session.send(self.reply_socket, 'object_info_reply',
308 308 oinfo, parent, ident)
309 309 logger.debug(msg)
310 310
311 def history_request(self, ident, parent):
312 # parent['content'] should contain keys "index", "raw", "output" and
313 # "this_session".
314 hist = self.shell.get_history(**parent['content'])
315 content = {'history' : hist}
316 msg = self.session.send(self.reply_socket, 'history_reply',
311 def history_tail_request(self, ident, parent):
312 # parent['content'] should contain keys "n", "raw" and "output"
313 hist = self.shell.history_manager.get_hist_tail(**parent['content'])
314 content = {'history' : list(hist)}
315 msg = self.session.send(self.reply_socket, 'history_tail_reply',
317 316 content, parent, ident)
318 317 logger.debug(str(msg))
319 318
320 319 def connect_request(self, ident, parent):
321 320 if self._recorded_ports is not None:
322 321 content = self._recorded_ports.copy()
323 322 else:
324 323 content = {}
325 324 msg = self.session.send(self.reply_socket, 'connect_reply',
326 325 content, parent, ident)
327 326 logger.debug(msg)
328 327
329 328 def shutdown_request(self, ident, parent):
330 329 self.shell.exit_now = True
331 330 self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent)
332 331 sys.exit(0)
333 332
334 333 #---------------------------------------------------------------------------
335 334 # Protected interface
336 335 #---------------------------------------------------------------------------
337 336
338 337 def _abort_queue(self):
339 338 while True:
340 339 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
341 340 if msg is None:
342 341 break
343 342 else:
344 343 assert ident is not None, \
345 344 "Unexpected missing message part."
346 345
347 346 logger.debug("Aborting:\n"+str(Message(msg)))
348 347 msg_type = msg['msg_type']
349 348 reply_type = msg_type.split('_')[0] + '_reply'
350 349 reply_msg = self.session.send(self.reply_socket, reply_type,
351 350 {'status' : 'aborted'}, msg, ident=ident)
352 351 logger.debug(reply_msg)
353 352 # We need to wait a bit for requests to come in. This can probably
354 353 # be set shorter for true asynchronous clients.
355 354 time.sleep(0.1)
356 355
357 356 def _raw_input(self, prompt, ident, parent):
358 357 # Flush output before making the request.
359 358 sys.stderr.flush()
360 359 sys.stdout.flush()
361 360
362 361 # Send the input request.
363 362 content = dict(prompt=prompt)
364 363 msg = self.session.send(self.req_socket, u'input_request', content, parent)
365 364
366 365 # Await a response.
367 366 ident, reply = self.session.recv(self.req_socket, 0)
368 367 try:
369 368 value = reply['content']['value']
370 369 except:
371 370 logger.error("Got bad raw_input reply: ")
372 371 logger.error(str(Message(parent)))
373 372 value = ''
374 373 return value
375 374
376 375 def _complete(self, msg):
377 376 c = msg['content']
378 377 try:
379 378 cpos = int(c['cursor_pos'])
380 379 except:
381 380 # If we don't get something that we can convert to an integer, at
382 381 # least attempt the completion guessing the cursor is at the end of
383 382 # the text, if there's any, and otherwise of the line
384 383 cpos = len(c['text'])
385 384 if cpos==0:
386 385 cpos = len(c['line'])
387 386 return self.shell.complete(c['text'], c['line'], cpos)
388 387
389 388 def _object_info(self, context):
390 389 symbol, leftover = self._symbol_from_context(context)
391 390 if symbol is not None and not leftover:
392 391 doc = getattr(symbol, '__doc__', '')
393 392 else:
394 393 doc = ''
395 394 object_info = dict(docstring = doc)
396 395 return object_info
397 396
398 397 def _symbol_from_context(self, context):
399 398 if not context:
400 399 return None, context
401 400
402 401 base_symbol_string = context[0]
403 402 symbol = self.shell.user_ns.get(base_symbol_string, None)
404 403 if symbol is None:
405 404 symbol = __builtin__.__dict__.get(base_symbol_string, None)
406 405 if symbol is None:
407 406 return None, context
408 407
409 408 context = context[1:]
410 409 for i, name in enumerate(context):
411 410 new_symbol = getattr(symbol, name, None)
412 411 if new_symbol is None:
413 412 return symbol, context[i:]
414 413 else:
415 414 symbol = new_symbol
416 415
417 416 return symbol, []
418 417
419 418 def _at_shutdown(self):
420 419 """Actions taken at shutdown by the kernel, called by python's atexit.
421 420 """
422 421 # io.rprint("Kernel at_shutdown") # dbg
423 422 if self._shutdown_message is not None:
424 423 self.session.send(self.reply_socket, self._shutdown_message)
425 424 self.session.send(self.pub_socket, self._shutdown_message)
426 425 logger.debug(str(self._shutdown_message))
427 426 # A very short sleep to give zmq time to flush its message buffers
428 427 # before Python truly shuts down.
429 428 time.sleep(0.01)
430 429
431 430
432 431 class QtKernel(Kernel):
433 432 """A Kernel subclass with Qt support."""
434 433
435 434 def start(self):
436 435 """Start a kernel with QtPy4 event loop integration."""
437 436
438 437 from PyQt4 import QtCore
439 438 from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
440 439
441 440 self.app = get_app_qt4([" "])
442 441 self.app.setQuitOnLastWindowClosed(False)
443 442 self.timer = QtCore.QTimer()
444 443 self.timer.timeout.connect(self.do_one_iteration)
445 444 # Units for the timer are in milliseconds
446 445 self.timer.start(1000*self._poll_interval)
447 446 start_event_loop_qt4(self.app)
448 447
449 448
450 449 class WxKernel(Kernel):
451 450 """A Kernel subclass with Wx support."""
452 451
453 452 def start(self):
454 453 """Start a kernel with wx event loop support."""
455 454
456 455 import wx
457 456 from IPython.lib.guisupport import start_event_loop_wx
458 457
459 458 doi = self.do_one_iteration
460 459 # Wx uses milliseconds
461 460 poll_interval = int(1000*self._poll_interval)
462 461
463 462 # We have to put the wx.Timer in a wx.Frame for it to fire properly.
464 463 # We make the Frame hidden when we create it in the main app below.
465 464 class TimerFrame(wx.Frame):
466 465 def __init__(self, func):
467 466 wx.Frame.__init__(self, None, -1)
468 467 self.timer = wx.Timer(self)
469 468 # Units for the timer are in milliseconds
470 469 self.timer.Start(poll_interval)
471 470 self.Bind(wx.EVT_TIMER, self.on_timer)
472 471 self.func = func
473 472
474 473 def on_timer(self, event):
475 474 self.func()
476 475
477 476 # We need a custom wx.App to create our Frame subclass that has the
478 477 # wx.Timer to drive the ZMQ event loop.
479 478 class IPWxApp(wx.App):
480 479 def OnInit(self):
481 480 self.frame = TimerFrame(doi)
482 481 self.frame.Show(False)
483 482 return True
484 483
485 484 # The redirect=False here makes sure that wx doesn't replace
486 485 # sys.stdout/stderr with its own classes.
487 486 self.app = IPWxApp(redirect=False)
488 487 start_event_loop_wx(self.app)
489 488
490 489
491 490 class TkKernel(Kernel):
492 491 """A Kernel subclass with Tk support."""
493 492
494 493 def start(self):
495 494 """Start a Tk enabled event loop."""
496 495
497 496 import Tkinter
498 497 doi = self.do_one_iteration
499 498 # Tk uses milliseconds
500 499 poll_interval = int(1000*self._poll_interval)
501 500 # For Tkinter, we create a Tk object and call its withdraw method.
502 501 class Timer(object):
503 502 def __init__(self, func):
504 503 self.app = Tkinter.Tk()
505 504 self.app.withdraw()
506 505 self.func = func
507 506
508 507 def on_timer(self):
509 508 self.func()
510 509 self.app.after(poll_interval, self.on_timer)
511 510
512 511 def start(self):
513 512 self.on_timer() # Call it once to get things going.
514 513 self.app.mainloop()
515 514
516 515 self.timer = Timer(doi)
517 516 self.timer.start()
518 517
519 518
520 519 class GTKKernel(Kernel):
521 520 """A Kernel subclass with GTK support."""
522 521
523 522 def start(self):
524 523 """Start the kernel, coordinating with the GTK event loop"""
525 524 from .gui.gtkembed import GTKEmbed
526 525
527 526 gtk_kernel = GTKEmbed(self)
528 527 gtk_kernel.start()
529 528
530 529
531 530 #-----------------------------------------------------------------------------
532 531 # Kernel main and launch functions
533 532 #-----------------------------------------------------------------------------
534 533
535 534 def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
536 535 independent=False, pylab=False, colors=None):
537 536 """Launches a localhost kernel, binding to the specified ports.
538 537
539 538 Parameters
540 539 ----------
541 540 ip : str, optional
542 541 The ip address the kernel will bind to.
543 542
544 543 xrep_port : int, optional
545 544 The port to use for XREP channel.
546 545
547 546 pub_port : int, optional
548 547 The port to use for the SUB channel.
549 548
550 549 req_port : int, optional
551 550 The port to use for the REQ (raw input) channel.
552 551
553 552 hb_port : int, optional
554 553 The port to use for the hearbeat REP channel.
555 554
556 555 independent : bool, optional (default False)
557 556 If set, the kernel process is guaranteed to survive if this process
558 557 dies. If not set, an effort is made to ensure that the kernel is killed
559 558 when this process dies. Note that in this case it is still good practice
560 559 to kill kernels manually before exiting.
561 560
562 561 pylab : bool or string, optional (default False)
563 562 If not False, the kernel will be launched with pylab enabled. If a
564 563 string is passed, matplotlib will use the specified backend. Otherwise,
565 564 matplotlib's default backend will be used.
566 565
567 566 colors : None or string, optional (default None)
568 567 If not None, specify the color scheme. One of (NoColor, LightBG, Linux)
569 568
570 569 Returns
571 570 -------
572 571 A tuple of form:
573 572 (kernel_process, xrep_port, pub_port, req_port)
574 573 where kernel_process is a Popen object and the ports are integers.
575 574 """
576 575 extra_arguments = []
577 576 if pylab:
578 577 extra_arguments.append('--pylab')
579 578 if isinstance(pylab, basestring):
580 579 extra_arguments.append(pylab)
581 580 if ip is not None:
582 581 extra_arguments.append('--ip')
583 582 if isinstance(ip, basestring):
584 583 extra_arguments.append(ip)
585 584 if colors is not None:
586 585 extra_arguments.append('--colors')
587 586 extra_arguments.append(colors)
588 587 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
589 588 xrep_port, pub_port, req_port, hb_port,
590 589 independent, extra_arguments)
591 590
592 591
593 592 def main():
594 593 """ The IPython kernel main entry point.
595 594 """
596 595 parser = make_argument_parser()
597 596 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
598 597 const='auto', help = \
599 598 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
600 599 given, the GUI backend is matplotlib's, otherwise use one of: \
601 600 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
602 601 parser.add_argument('--colors',
603 602 type=str, dest='colors',
604 603 help="Set the color scheme (NoColor, Linux, and LightBG).",
605 604 metavar='ZMQInteractiveShell.colors')
606 605 namespace = parser.parse_args()
607 606
608 607 kernel_class = Kernel
609 608
610 609 kernel_classes = {
611 610 'qt' : QtKernel,
612 611 'qt4': QtKernel,
613 612 'inline': Kernel,
614 613 'wx' : WxKernel,
615 614 'tk' : TkKernel,
616 615 'gtk': GTKKernel,
617 616 }
618 617 if namespace.pylab:
619 618 if namespace.pylab == 'auto':
620 619 gui, backend = pylabtools.find_gui_and_backend()
621 620 else:
622 621 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
623 622 kernel_class = kernel_classes.get(gui)
624 623 if kernel_class is None:
625 624 raise ValueError('GUI is not supported: %r' % gui)
626 625 pylabtools.activate_matplotlib(backend)
627 626 if namespace.colors:
628 627 ZMQInteractiveShell.colors=namespace.colors
629 628
630 629 kernel = make_kernel(namespace, kernel_class, OutStream)
631 630
632 631 if namespace.pylab:
633 632 pylabtools.import_pylab(kernel.shell.user_ns, backend,
634 633 shell=kernel.shell)
635 634
636 635 start_kernel(namespace, kernel)
637 636
638 637
639 638 if __name__ == '__main__':
640 639 main()
@@ -1,914 +1,908 b''
1 1 """Base classes to manage the interaction with a running kernel.
2 2
3 3 TODO
4 4 * Create logger to handle debugging and console messages.
5 5 """
6 6
7 7 #-----------------------------------------------------------------------------
8 8 # Copyright (C) 2008-2010 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 # Standard library imports.
19 19 import atexit
20 20 from Queue import Queue, Empty
21 21 from subprocess import Popen
22 22 import signal
23 23 import sys
24 24 from threading import Thread
25 25 import time
26 26 import logging
27 27
28 28 # System library imports.
29 29 import zmq
30 30 from zmq import POLLIN, POLLOUT, POLLERR
31 31 from zmq.eventloop import ioloop
32 32
33 33 # Local imports.
34 34 from IPython.utils import io
35 35 from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS
36 36 from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress
37 37 from session import Session, Message
38 38
39 39 #-----------------------------------------------------------------------------
40 40 # Constants and exceptions
41 41 #-----------------------------------------------------------------------------
42 42
43 43 class InvalidPortNumber(Exception):
44 44 pass
45 45
46 46 #-----------------------------------------------------------------------------
47 47 # Utility functions
48 48 #-----------------------------------------------------------------------------
49 49
50 50 # some utilities to validate message structure, these might get moved elsewhere
51 51 # if they prove to have more generic utility
52 52
53 53 def validate_string_list(lst):
54 54 """Validate that the input is a list of strings.
55 55
56 56 Raises ValueError if not."""
57 57 if not isinstance(lst, list):
58 58 raise ValueError('input %r must be a list' % lst)
59 59 for x in lst:
60 60 if not isinstance(x, basestring):
61 61 raise ValueError('element %r in list must be a string' % x)
62 62
63 63
64 64 def validate_string_dict(dct):
65 65 """Validate that the input is a dict with string keys and values.
66 66
67 67 Raises ValueError if not."""
68 68 for k,v in dct.iteritems():
69 69 if not isinstance(k, basestring):
70 70 raise ValueError('key %r in dict must be a string' % k)
71 71 if not isinstance(v, basestring):
72 72 raise ValueError('value %r in dict must be a string' % v)
73 73
74 74
75 75 #-----------------------------------------------------------------------------
76 76 # ZMQ Socket Channel classes
77 77 #-----------------------------------------------------------------------------
78 78
79 79 class ZmqSocketChannel(Thread):
80 80 """The base class for the channels that use ZMQ sockets.
81 81 """
82 82 context = None
83 83 session = None
84 84 socket = None
85 85 ioloop = None
86 86 iostate = None
87 87 _address = None
88 88
89 89 def __init__(self, context, session, address):
90 90 """Create a channel
91 91
92 92 Parameters
93 93 ----------
94 94 context : :class:`zmq.Context`
95 95 The ZMQ context to use.
96 96 session : :class:`session.Session`
97 97 The session to use.
98 98 address : tuple
99 99 Standard (ip, port) tuple that the kernel is listening on.
100 100 """
101 101 super(ZmqSocketChannel, self).__init__()
102 102 self.daemon = True
103 103
104 104 self.context = context
105 105 self.session = session
106 106 if address[1] == 0:
107 107 message = 'The port number for a channel cannot be 0.'
108 108 raise InvalidPortNumber(message)
109 109 self._address = address
110 110
111 111 def stop(self):
112 112 """Stop the channel's activity.
113 113
114 114 This calls :method:`Thread.join` and returns when the thread
115 115 terminates. :class:`RuntimeError` will be raised if
116 116 :method:`self.start` is called again.
117 117 """
118 118 self.join()
119 119
120 120 @property
121 121 def address(self):
122 122 """Get the channel's address as an (ip, port) tuple.
123 123
124 124 By the default, the address is (localhost, 0), where 0 means a random
125 125 port.
126 126 """
127 127 return self._address
128 128
129 129 def add_io_state(self, state):
130 130 """Add IO state to the eventloop.
131 131
132 132 Parameters
133 133 ----------
134 134 state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR
135 135 The IO state flag to set.
136 136
137 137 This is thread safe as it uses the thread safe IOLoop.add_callback.
138 138 """
139 139 def add_io_state_callback():
140 140 if not self.iostate & state:
141 141 self.iostate = self.iostate | state
142 142 self.ioloop.update_handler(self.socket, self.iostate)
143 143 self.ioloop.add_callback(add_io_state_callback)
144 144
145 145 def drop_io_state(self, state):
146 146 """Drop IO state from the eventloop.
147 147
148 148 Parameters
149 149 ----------
150 150 state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR
151 151 The IO state flag to set.
152 152
153 153 This is thread safe as it uses the thread safe IOLoop.add_callback.
154 154 """
155 155 def drop_io_state_callback():
156 156 if self.iostate & state:
157 157 self.iostate = self.iostate & (~state)
158 158 self.ioloop.update_handler(self.socket, self.iostate)
159 159 self.ioloop.add_callback(drop_io_state_callback)
160 160
161 161
162 162 class XReqSocketChannel(ZmqSocketChannel):
163 163 """The XREQ channel for issues request/replies to the kernel.
164 164 """
165 165
166 166 command_queue = None
167 167
168 168 def __init__(self, context, session, address):
169 169 super(XReqSocketChannel, self).__init__(context, session, address)
170 170 self.command_queue = Queue()
171 171 self.ioloop = ioloop.IOLoop()
172 172
173 173 def run(self):
174 174 """The thread's main activity. Call start() instead."""
175 175 self.socket = self.context.socket(zmq.XREQ)
176 176 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
177 177 self.socket.connect('tcp://%s:%i' % self.address)
178 178 self.iostate = POLLERR|POLLIN
179 179 self.ioloop.add_handler(self.socket, self._handle_events,
180 180 self.iostate)
181 181 self.ioloop.start()
182 182
183 183 def stop(self):
184 184 self.ioloop.stop()
185 185 super(XReqSocketChannel, self).stop()
186 186
187 187 def call_handlers(self, msg):
188 188 """This method is called in the ioloop thread when a message arrives.
189 189
190 190 Subclasses should override this method to handle incoming messages.
191 191 It is important to remember that this method is called in the thread
192 192 so that some logic must be done to ensure that the application leve
193 193 handlers are called in the application thread.
194 194 """
195 195 raise NotImplementedError('call_handlers must be defined in a subclass.')
196 196
197 197 def execute(self, code, silent=False,
198 198 user_variables=None, user_expressions=None):
199 199 """Execute code in the kernel.
200 200
201 201 Parameters
202 202 ----------
203 203 code : str
204 204 A string of Python code.
205 205
206 206 silent : bool, optional (default False)
207 207 If set, the kernel will execute the code as quietly possible.
208 208
209 209 user_variables : list, optional
210 210 A list of variable names to pull from the user's namespace. They
211 211 will come back as a dict with these names as keys and their
212 212 :func:`repr` as values.
213 213
214 214 user_expressions : dict, optional
215 215 A dict with string keys and to pull from the user's
216 216 namespace. They will come back as a dict with these names as keys
217 217 and their :func:`repr` as values.
218 218
219 219 Returns
220 220 -------
221 221 The msg_id of the message sent.
222 222 """
223 223 if user_variables is None:
224 224 user_variables = []
225 225 if user_expressions is None:
226 226 user_expressions = {}
227 227
228 228 # Don't waste network traffic if inputs are invalid
229 229 if not isinstance(code, basestring):
230 230 raise ValueError('code %r must be a string' % code)
231 231 validate_string_list(user_variables)
232 232 validate_string_dict(user_expressions)
233 233
234 234 # Create class for content/msg creation. Related to, but possibly
235 235 # not in Session.
236 236 content = dict(code=code, silent=silent,
237 237 user_variables=user_variables,
238 238 user_expressions=user_expressions)
239 239 msg = self.session.msg('execute_request', content)
240 240 self._queue_request(msg)
241 241 return msg['header']['msg_id']
242 242
243 243 def complete(self, text, line, cursor_pos, block=None):
244 244 """Tab complete text in the kernel's namespace.
245 245
246 246 Parameters
247 247 ----------
248 248 text : str
249 249 The text to complete.
250 250 line : str
251 251 The full line of text that is the surrounding context for the
252 252 text to complete.
253 253 cursor_pos : int
254 254 The position of the cursor in the line where the completion was
255 255 requested.
256 256 block : str, optional
257 257 The full block of code in which the completion is being requested.
258 258
259 259 Returns
260 260 -------
261 261 The msg_id of the message sent.
262 262 """
263 263 content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos)
264 264 msg = self.session.msg('complete_request', content)
265 265 self._queue_request(msg)
266 266 return msg['header']['msg_id']
267 267
268 268 def object_info(self, oname):
269 269 """Get metadata information about an object.
270 270
271 271 Parameters
272 272 ----------
273 273 oname : str
274 274 A string specifying the object name.
275 275
276 276 Returns
277 277 -------
278 278 The msg_id of the message sent.
279 279 """
280 280 content = dict(oname=oname)
281 281 msg = self.session.msg('object_info_request', content)
282 282 self._queue_request(msg)
283 283 return msg['header']['msg_id']
284 284
285 def history(self, index=None, raw=False, output=True, this_session=True):
285 def history_tail(self, n=10, raw=True, output=False):
286 286 """Get the history list.
287 287
288 288 Parameters
289 289 ----------
290 index : n or (n1, n2) or None
291 If n, then the last entries. If a tuple, then all in
292 range(n1, n2). If None, then all entries. Raises IndexError if
293 the format of index is incorrect.
290 n : int
291 The number of lines of history to get.
294 292 raw : bool
295 293 If True, return the raw input.
296 294 output : bool
297 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 297 Returns
303 298 -------
304 299 The msg_id of the message sent.
305 300 """
306 content = dict(index=index, raw=raw, output=output,
307 this_session=this_session)
308 msg = self.session.msg('history_request', content)
301 content = dict(n=n, raw=raw, output=output)
302 msg = self.session.msg('history_tail_request', content)
309 303 self._queue_request(msg)
310 304 return msg['header']['msg_id']
311 305
312 306 def shutdown(self, restart=False):
313 307 """Request an immediate kernel shutdown.
314 308
315 309 Upon receipt of the (empty) reply, client code can safely assume that
316 310 the kernel has shut down and it's safe to forcefully terminate it if
317 311 it's still alive.
318 312
319 313 The kernel will send the reply via a function registered with Python's
320 314 atexit module, ensuring it's truly done as the kernel is done with all
321 315 normal operation.
322 316 """
323 317 # Send quit message to kernel. Once we implement kernel-side setattr,
324 318 # this should probably be done that way, but for now this will do.
325 319 msg = self.session.msg('shutdown_request', {'restart':restart})
326 320 self._queue_request(msg)
327 321 return msg['header']['msg_id']
328 322
329 323 def _handle_events(self, socket, events):
330 324 if events & POLLERR:
331 325 self._handle_err()
332 326 if events & POLLOUT:
333 327 self._handle_send()
334 328 if events & POLLIN:
335 329 self._handle_recv()
336 330
337 331 def _handle_recv(self):
338 332 ident,msg = self.session.recv(self.socket, 0)
339 333 self.call_handlers(msg)
340 334
341 335 def _handle_send(self):
342 336 try:
343 337 msg = self.command_queue.get(False)
344 338 except Empty:
345 339 pass
346 340 else:
347 341 self.session.send(self.socket,msg)
348 342 if self.command_queue.empty():
349 343 self.drop_io_state(POLLOUT)
350 344
351 345 def _handle_err(self):
352 346 # We don't want to let this go silently, so eventually we should log.
353 347 raise zmq.ZMQError()
354 348
355 349 def _queue_request(self, msg):
356 350 self.command_queue.put(msg)
357 351 self.add_io_state(POLLOUT)
358 352
359 353
360 354 class SubSocketChannel(ZmqSocketChannel):
361 355 """The SUB channel which listens for messages that the kernel publishes.
362 356 """
363 357
364 358 def __init__(self, context, session, address):
365 359 super(SubSocketChannel, self).__init__(context, session, address)
366 360 self.ioloop = ioloop.IOLoop()
367 361
368 362 def run(self):
369 363 """The thread's main activity. Call start() instead."""
370 364 self.socket = self.context.socket(zmq.SUB)
371 365 self.socket.setsockopt(zmq.SUBSCRIBE,'')
372 366 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
373 367 self.socket.connect('tcp://%s:%i' % self.address)
374 368 self.iostate = POLLIN|POLLERR
375 369 self.ioloop.add_handler(self.socket, self._handle_events,
376 370 self.iostate)
377 371 self.ioloop.start()
378 372
379 373 def stop(self):
380 374 self.ioloop.stop()
381 375 super(SubSocketChannel, self).stop()
382 376
383 377 def call_handlers(self, msg):
384 378 """This method is called in the ioloop thread when a message arrives.
385 379
386 380 Subclasses should override this method to handle incoming messages.
387 381 It is important to remember that this method is called in the thread
388 382 so that some logic must be done to ensure that the application leve
389 383 handlers are called in the application thread.
390 384 """
391 385 raise NotImplementedError('call_handlers must be defined in a subclass.')
392 386
393 387 def flush(self, timeout=1.0):
394 388 """Immediately processes all pending messages on the SUB channel.
395 389
396 390 Callers should use this method to ensure that :method:`call_handlers`
397 391 has been called for all messages that have been received on the
398 392 0MQ SUB socket of this channel.
399 393
400 394 This method is thread safe.
401 395
402 396 Parameters
403 397 ----------
404 398 timeout : float, optional
405 399 The maximum amount of time to spend flushing, in seconds. The
406 400 default is one second.
407 401 """
408 402 # We do the IOLoop callback process twice to ensure that the IOLoop
409 403 # gets to perform at least one full poll.
410 404 stop_time = time.time() + timeout
411 405 for i in xrange(2):
412 406 self._flushed = False
413 407 self.ioloop.add_callback(self._flush)
414 408 while not self._flushed and time.time() < stop_time:
415 409 time.sleep(0.01)
416 410
417 411 def _handle_events(self, socket, events):
418 412 # Turn on and off POLLOUT depending on if we have made a request
419 413 if events & POLLERR:
420 414 self._handle_err()
421 415 if events & POLLIN:
422 416 self._handle_recv()
423 417
424 418 def _handle_err(self):
425 419 # We don't want to let this go silently, so eventually we should log.
426 420 raise zmq.ZMQError()
427 421
428 422 def _handle_recv(self):
429 423 # Get all of the messages we can
430 424 while True:
431 425 try:
432 426 ident,msg = self.session.recv(self.socket)
433 427 except zmq.ZMQError:
434 428 # Check the errno?
435 429 # Will this trigger POLLERR?
436 430 break
437 431 else:
438 432 if msg is None:
439 433 break
440 434 self.call_handlers(msg)
441 435
442 436 def _flush(self):
443 437 """Callback for :method:`self.flush`."""
444 438 self._flushed = True
445 439
446 440
447 441 class RepSocketChannel(ZmqSocketChannel):
448 442 """A reply channel to handle raw_input requests that the kernel makes."""
449 443
450 444 msg_queue = None
451 445
452 446 def __init__(self, context, session, address):
453 447 super(RepSocketChannel, self).__init__(context, session, address)
454 448 self.ioloop = ioloop.IOLoop()
455 449 self.msg_queue = Queue()
456 450
457 451 def run(self):
458 452 """The thread's main activity. Call start() instead."""
459 453 self.socket = self.context.socket(zmq.XREQ)
460 454 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
461 455 self.socket.connect('tcp://%s:%i' % self.address)
462 456 self.iostate = POLLERR|POLLIN
463 457 self.ioloop.add_handler(self.socket, self._handle_events,
464 458 self.iostate)
465 459 self.ioloop.start()
466 460
467 461 def stop(self):
468 462 self.ioloop.stop()
469 463 super(RepSocketChannel, self).stop()
470 464
471 465 def call_handlers(self, msg):
472 466 """This method is called in the ioloop thread when a message arrives.
473 467
474 468 Subclasses should override this method to handle incoming messages.
475 469 It is important to remember that this method is called in the thread
476 470 so that some logic must be done to ensure that the application leve
477 471 handlers are called in the application thread.
478 472 """
479 473 raise NotImplementedError('call_handlers must be defined in a subclass.')
480 474
481 475 def input(self, string):
482 476 """Send a string of raw input to the kernel."""
483 477 content = dict(value=string)
484 478 msg = self.session.msg('input_reply', content)
485 479 self._queue_reply(msg)
486 480
487 481 def _handle_events(self, socket, events):
488 482 if events & POLLERR:
489 483 self._handle_err()
490 484 if events & POLLOUT:
491 485 self._handle_send()
492 486 if events & POLLIN:
493 487 self._handle_recv()
494 488
495 489 def _handle_recv(self):
496 490 ident,msg = self.session.recv(self.socket, 0)
497 491 self.call_handlers(msg)
498 492
499 493 def _handle_send(self):
500 494 try:
501 495 msg = self.msg_queue.get(False)
502 496 except Empty:
503 497 pass
504 498 else:
505 499 self.session.send(self.socket,msg)
506 500 if self.msg_queue.empty():
507 501 self.drop_io_state(POLLOUT)
508 502
509 503 def _handle_err(self):
510 504 # We don't want to let this go silently, so eventually we should log.
511 505 raise zmq.ZMQError()
512 506
513 507 def _queue_reply(self, msg):
514 508 self.msg_queue.put(msg)
515 509 self.add_io_state(POLLOUT)
516 510
517 511
518 512 class HBSocketChannel(ZmqSocketChannel):
519 513 """The heartbeat channel which monitors the kernel heartbeat.
520 514
521 515 Note that the heartbeat channel is paused by default. As long as you start
522 516 this channel, the kernel manager will ensure that it is paused and un-paused
523 517 as appropriate.
524 518 """
525 519
526 520 time_to_dead = 3.0
527 521 socket = None
528 522 poller = None
529 523 _running = None
530 524 _pause = None
531 525
532 526 def __init__(self, context, session, address):
533 527 super(HBSocketChannel, self).__init__(context, session, address)
534 528 self._running = False
535 529 self._pause = True
536 530
537 531 def _create_socket(self):
538 532 self.socket = self.context.socket(zmq.REQ)
539 533 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
540 534 self.socket.connect('tcp://%s:%i' % self.address)
541 535 self.poller = zmq.Poller()
542 536 self.poller.register(self.socket, zmq.POLLIN)
543 537
544 538 def run(self):
545 539 """The thread's main activity. Call start() instead."""
546 540 self._create_socket()
547 541 self._running = True
548 542 while self._running:
549 543 if self._pause:
550 544 time.sleep(self.time_to_dead)
551 545 else:
552 546 since_last_heartbeat = 0.0
553 547 request_time = time.time()
554 548 try:
555 549 #io.rprint('Ping from HB channel') # dbg
556 550 self.socket.send(b'ping')
557 551 except zmq.ZMQError, e:
558 552 #io.rprint('*** HB Error:', e) # dbg
559 553 if e.errno == zmq.EFSM:
560 554 #io.rprint('sleep...', self.time_to_dead) # dbg
561 555 time.sleep(self.time_to_dead)
562 556 self._create_socket()
563 557 else:
564 558 raise
565 559 else:
566 560 while True:
567 561 try:
568 562 self.socket.recv(zmq.NOBLOCK)
569 563 except zmq.ZMQError, e:
570 564 #io.rprint('*** HB Error 2:', e) # dbg
571 565 if e.errno == zmq.EAGAIN:
572 566 before_poll = time.time()
573 567 until_dead = self.time_to_dead - (before_poll -
574 568 request_time)
575 569
576 570 # When the return value of poll() is an empty
577 571 # list, that is when things have gone wrong
578 572 # (zeromq bug). As long as it is not an empty
579 573 # list, poll is working correctly even if it
580 574 # returns quickly. Note: poll timeout is in
581 575 # milliseconds.
582 576 self.poller.poll(1000*until_dead)
583 577
584 578 since_last_heartbeat = time.time()-request_time
585 579 if since_last_heartbeat > self.time_to_dead:
586 580 self.call_handlers(since_last_heartbeat)
587 581 break
588 582 else:
589 583 # FIXME: We should probably log this instead.
590 584 raise
591 585 else:
592 586 until_dead = self.time_to_dead - (time.time() -
593 587 request_time)
594 588 if until_dead > 0.0:
595 589 #io.rprint('sleep...', self.time_to_dead) # dbg
596 590 time.sleep(until_dead)
597 591 break
598 592
599 593 def pause(self):
600 594 """Pause the heartbeat."""
601 595 self._pause = True
602 596
603 597 def unpause(self):
604 598 """Unpause the heartbeat."""
605 599 self._pause = False
606 600
607 601 def is_beating(self):
608 602 """Is the heartbeat running and not paused."""
609 603 if self.is_alive() and not self._pause:
610 604 return True
611 605 else:
612 606 return False
613 607
614 608 def stop(self):
615 609 self._running = False
616 610 super(HBSocketChannel, self).stop()
617 611
618 612 def call_handlers(self, since_last_heartbeat):
619 613 """This method is called in the ioloop thread when a message arrives.
620 614
621 615 Subclasses should override this method to handle incoming messages.
622 616 It is important to remember that this method is called in the thread
623 617 so that some logic must be done to ensure that the application leve
624 618 handlers are called in the application thread.
625 619 """
626 620 raise NotImplementedError('call_handlers must be defined in a subclass.')
627 621
628 622
629 623 #-----------------------------------------------------------------------------
630 624 # Main kernel manager class
631 625 #-----------------------------------------------------------------------------
632 626
633 627 class KernelManager(HasTraits):
634 628 """ Manages a kernel for a frontend.
635 629
636 630 The SUB channel is for the frontend to receive messages published by the
637 631 kernel.
638 632
639 633 The REQ channel is for the frontend to make requests of the kernel.
640 634
641 635 The REP channel is for the kernel to request stdin (raw_input) from the
642 636 frontend.
643 637 """
644 638 # The PyZMQ Context to use for communication with the kernel.
645 639 context = Instance(zmq.Context,(),{})
646 640
647 641 # The Session to use for communication with the kernel.
648 642 session = Instance(Session,(),{})
649 643
650 644 # The kernel process with which the KernelManager is communicating.
651 645 kernel = Instance(Popen)
652 646
653 647 # The addresses for the communication channels.
654 648 xreq_address = TCPAddress((LOCALHOST, 0))
655 649 sub_address = TCPAddress((LOCALHOST, 0))
656 650 rep_address = TCPAddress((LOCALHOST, 0))
657 651 hb_address = TCPAddress((LOCALHOST, 0))
658 652
659 653 # The classes to use for the various channels.
660 654 xreq_channel_class = Type(XReqSocketChannel)
661 655 sub_channel_class = Type(SubSocketChannel)
662 656 rep_channel_class = Type(RepSocketChannel)
663 657 hb_channel_class = Type(HBSocketChannel)
664 658
665 659 # Protected traits.
666 660 _launch_args = Any
667 661 _xreq_channel = Any
668 662 _sub_channel = Any
669 663 _rep_channel = Any
670 664 _hb_channel = Any
671 665
672 666 def __init__(self, **kwargs):
673 667 super(KernelManager, self).__init__(**kwargs)
674 668 # Uncomment this to try closing the context.
675 669 # atexit.register(self.context.close)
676 670
677 671 #--------------------------------------------------------------------------
678 672 # Channel management methods:
679 673 #--------------------------------------------------------------------------
680 674
681 675 def start_channels(self, xreq=True, sub=True, rep=True, hb=True):
682 676 """Starts the channels for this kernel.
683 677
684 678 This will create the channels if they do not exist and then start
685 679 them. If port numbers of 0 are being used (random ports) then you
686 680 must first call :method:`start_kernel`. If the channels have been
687 681 stopped and you call this, :class:`RuntimeError` will be raised.
688 682 """
689 683 if xreq:
690 684 self.xreq_channel.start()
691 685 if sub:
692 686 self.sub_channel.start()
693 687 if rep:
694 688 self.rep_channel.start()
695 689 if hb:
696 690 self.hb_channel.start()
697 691
698 692 def stop_channels(self):
699 693 """Stops all the running channels for this kernel.
700 694 """
701 695 if self.xreq_channel.is_alive():
702 696 self.xreq_channel.stop()
703 697 if self.sub_channel.is_alive():
704 698 self.sub_channel.stop()
705 699 if self.rep_channel.is_alive():
706 700 self.rep_channel.stop()
707 701 if self.hb_channel.is_alive():
708 702 self.hb_channel.stop()
709 703
710 704 @property
711 705 def channels_running(self):
712 706 """Are any of the channels created and running?"""
713 707 return (self.xreq_channel.is_alive() or self.sub_channel.is_alive() or
714 708 self.rep_channel.is_alive() or self.hb_channel.is_alive())
715 709
716 710 #--------------------------------------------------------------------------
717 711 # Kernel process management methods:
718 712 #--------------------------------------------------------------------------
719 713
720 714 def start_kernel(self, **kw):
721 715 """Starts a kernel process and configures the manager to use it.
722 716
723 717 If random ports (port=0) are being used, this method must be called
724 718 before the channels are created.
725 719
726 720 Parameters:
727 721 -----------
728 722 ipython : bool, optional (default True)
729 723 Whether to use an IPython kernel instead of a plain Python kernel.
730 724 """
731 725 xreq, sub, rep, hb = self.xreq_address, self.sub_address, \
732 726 self.rep_address, self.hb_address
733 727 if xreq[0] not in LOCAL_IPS or sub[0] not in LOCAL_IPS or \
734 728 rep[0] not in LOCAL_IPS or hb[0] not in LOCAL_IPS:
735 729 raise RuntimeError("Can only launch a kernel on a local interface. "
736 730 "Make sure that the '*_address' attributes are "
737 731 "configured properly. "
738 732 "Currently valid addresses are: %s"%LOCAL_IPS
739 733 )
740 734
741 735 self._launch_args = kw.copy()
742 736 if kw.pop('ipython', True):
743 737 from ipkernel import launch_kernel
744 738 else:
745 739 from pykernel import launch_kernel
746 740 self.kernel, xrep, pub, req, _hb = launch_kernel(
747 741 xrep_port=xreq[1], pub_port=sub[1],
748 742 req_port=rep[1], hb_port=hb[1], **kw)
749 743 self.xreq_address = (xreq[0], xrep)
750 744 self.sub_address = (sub[0], pub)
751 745 self.rep_address = (rep[0], req)
752 746 self.hb_address = (hb[0], _hb)
753 747
754 748 def shutdown_kernel(self, restart=False):
755 749 """ Attempts to the stop the kernel process cleanly. If the kernel
756 750 cannot be stopped, it is killed, if possible.
757 751 """
758 752 # FIXME: Shutdown does not work on Windows due to ZMQ errors!
759 753 if sys.platform == 'win32':
760 754 self.kill_kernel()
761 755 return
762 756
763 757 # Pause the heart beat channel if it exists.
764 758 if self._hb_channel is not None:
765 759 self._hb_channel.pause()
766 760
767 761 # Don't send any additional kernel kill messages immediately, to give
768 762 # the kernel a chance to properly execute shutdown actions. Wait for at
769 763 # most 1s, checking every 0.1s.
770 764 self.xreq_channel.shutdown(restart=restart)
771 765 for i in range(10):
772 766 if self.is_alive:
773 767 time.sleep(0.1)
774 768 else:
775 769 break
776 770 else:
777 771 # OK, we've waited long enough.
778 772 if self.has_kernel:
779 773 self.kill_kernel()
780 774
781 775 def restart_kernel(self, now=False):
782 776 """Restarts a kernel with the same arguments that were used to launch
783 777 it. If the old kernel was launched with random ports, the same ports
784 778 will be used for the new kernel.
785 779
786 780 Parameters
787 781 ----------
788 782 now : bool, optional
789 783 If True, the kernel is forcefully restarted *immediately*, without
790 784 having a chance to do any cleanup action. Otherwise the kernel is
791 785 given 1s to clean up before a forceful restart is issued.
792 786
793 787 In all cases the kernel is restarted, the only difference is whether
794 788 it is given a chance to perform a clean shutdown or not.
795 789 """
796 790 if self._launch_args is None:
797 791 raise RuntimeError("Cannot restart the kernel. "
798 792 "No previous call to 'start_kernel'.")
799 793 else:
800 794 if self.has_kernel:
801 795 if now:
802 796 self.kill_kernel()
803 797 else:
804 798 self.shutdown_kernel(restart=True)
805 799 self.start_kernel(**self._launch_args)
806 800
807 801 # FIXME: Messages get dropped in Windows due to probable ZMQ bug
808 802 # unless there is some delay here.
809 803 if sys.platform == 'win32':
810 804 time.sleep(0.2)
811 805
812 806 @property
813 807 def has_kernel(self):
814 808 """Returns whether a kernel process has been specified for the kernel
815 809 manager.
816 810 """
817 811 return self.kernel is not None
818 812
819 813 def kill_kernel(self):
820 814 """ Kill the running kernel. """
821 815 if self.has_kernel:
822 816 # Pause the heart beat channel if it exists.
823 817 if self._hb_channel is not None:
824 818 self._hb_channel.pause()
825 819
826 820 # Attempt to kill the kernel.
827 821 try:
828 822 self.kernel.kill()
829 823 except OSError, e:
830 824 # In Windows, we will get an Access Denied error if the process
831 825 # has already terminated. Ignore it.
832 826 if not (sys.platform == 'win32' and e.winerror == 5):
833 827 raise
834 828 self.kernel = None
835 829 else:
836 830 raise RuntimeError("Cannot kill kernel. No kernel is running!")
837 831
838 832 def interrupt_kernel(self):
839 833 """ Interrupts the kernel. Unlike ``signal_kernel``, this operation is
840 834 well supported on all platforms.
841 835 """
842 836 if self.has_kernel:
843 837 if sys.platform == 'win32':
844 838 from parentpoller import ParentPollerWindows as Poller
845 839 Poller.send_interrupt(self.kernel.win32_interrupt_event)
846 840 else:
847 841 self.kernel.send_signal(signal.SIGINT)
848 842 else:
849 843 raise RuntimeError("Cannot interrupt kernel. No kernel is running!")
850 844
851 845 def signal_kernel(self, signum):
852 846 """ Sends a signal to the kernel. Note that since only SIGTERM is
853 847 supported on Windows, this function is only useful on Unix systems.
854 848 """
855 849 if self.has_kernel:
856 850 self.kernel.send_signal(signum)
857 851 else:
858 852 raise RuntimeError("Cannot signal kernel. No kernel is running!")
859 853
860 854 @property
861 855 def is_alive(self):
862 856 """Is the kernel process still running?"""
863 857 # FIXME: not using a heartbeat means this method is broken for any
864 858 # remote kernel, it's only capable of handling local kernels.
865 859 if self.has_kernel:
866 860 if self.kernel.poll() is None:
867 861 return True
868 862 else:
869 863 return False
870 864 else:
871 865 # We didn't start the kernel with this KernelManager so we don't
872 866 # know if it is running. We should use a heartbeat for this case.
873 867 return True
874 868
875 869 #--------------------------------------------------------------------------
876 870 # Channels used for communication with the kernel:
877 871 #--------------------------------------------------------------------------
878 872
879 873 @property
880 874 def xreq_channel(self):
881 875 """Get the REQ socket channel object to make requests of the kernel."""
882 876 if self._xreq_channel is None:
883 877 self._xreq_channel = self.xreq_channel_class(self.context,
884 878 self.session,
885 879 self.xreq_address)
886 880 return self._xreq_channel
887 881
888 882 @property
889 883 def sub_channel(self):
890 884 """Get the SUB socket channel object."""
891 885 if self._sub_channel is None:
892 886 self._sub_channel = self.sub_channel_class(self.context,
893 887 self.session,
894 888 self.sub_address)
895 889 return self._sub_channel
896 890
897 891 @property
898 892 def rep_channel(self):
899 893 """Get the REP socket channel object to handle stdin (raw_input)."""
900 894 if self._rep_channel is None:
901 895 self._rep_channel = self.rep_channel_class(self.context,
902 896 self.session,
903 897 self.rep_address)
904 898 return self._rep_channel
905 899
906 900 @property
907 901 def hb_channel(self):
908 902 """Get the heartbeat socket channel object to check that the
909 903 kernel is alive."""
910 904 if self._hb_channel is None:
911 905 self._hb_channel = self.hb_channel_class(self.context,
912 906 self.session,
913 907 self.hb_address)
914 908 return self._hb_channel
General Comments 0
You need to be logged in to leave comments. Login now