##// END OF EJS Templates
Merge pull request #4649 from arunpersaud/master...
Thomas Kluyver -
r13842:10151c20 merge
parent child Browse files
Show More
@@ -1,691 +1,698 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Subclass of InteractiveShell for terminal based frontends."""
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 from __future__ import print_function
17 17
18 18 import bdb
19 19 import os
20 20 import sys
21 21
22 22 from IPython.core.error import TryNext, UsageError
23 23 from IPython.core.usage import interactive_usage, default_banner
24 24 from IPython.core.inputsplitter import IPythonInputSplitter
25 25 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
26 26 from IPython.core.magic import Magics, magics_class, line_magic
27 27 from IPython.lib.clipboard import ClipboardEmpty
28 28 from IPython.testing.skipdoctest import skip_doctest
29 29 from IPython.utils.encoding import get_stream_enc
30 30 from IPython.utils import py3compat
31 31 from IPython.utils.terminal import toggle_set_term_title, set_term_title
32 32 from IPython.utils.process import abbrev_cwd
33 33 from IPython.utils.warn import warn, error
34 34 from IPython.utils.text import num_ini_spaces, SList, strip_email_quotes
35 35 from IPython.utils.traitlets import Integer, CBool, Unicode
36 36
37 37 #-----------------------------------------------------------------------------
38 38 # Utilities
39 39 #-----------------------------------------------------------------------------
40 40
41 41 def get_default_editor():
42 42 try:
43 43 ed = os.environ['EDITOR']
44 44 if not py3compat.PY3:
45 45 ed = ed.decode()
46 46 return ed
47 47 except KeyError:
48 48 pass
49 49 except UnicodeError:
50 50 warn("$EDITOR environment variable is not pure ASCII. Using platform "
51 51 "default editor.")
52 52
53 53 if os.name == 'posix':
54 54 return 'vi' # the only one guaranteed to be there!
55 55 else:
56 56 return 'notepad' # same in Windows!
57 57
58 def get_pasted_lines(sentinel, l_input=py3compat.input):
58 def get_pasted_lines(sentinel, l_input=py3compat.input, quiet=False):
59 59 """ Yield pasted lines until the user enters the given sentinel value.
60 60 """
61 print("Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
62 % sentinel)
61 if not quiet:
62 print("Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
63 % sentinel)
64 prompt = ":"
65 else:
66 prompt = ""
63 67 while True:
64 68 try:
65 l = l_input(':')
69 l = l_input(prompt)
66 70 if l == sentinel:
67 71 return
68 72 else:
69 73 yield l
70 74 except EOFError:
71 75 print('<EOF>')
72 76 return
73 77
74 78
75 79 #------------------------------------------------------------------------
76 80 # Terminal-specific magics
77 81 #------------------------------------------------------------------------
78 82
79 83 @magics_class
80 84 class TerminalMagics(Magics):
81 85 def __init__(self, shell):
82 86 super(TerminalMagics, self).__init__(shell)
83 87 self.input_splitter = IPythonInputSplitter()
84 88
85 89 def store_or_execute(self, block, name):
86 90 """ Execute a block, or store it in a variable, per the user's request.
87 91 """
88 92 if name:
89 93 # If storing it for further editing
90 94 self.shell.user_ns[name] = SList(block.splitlines())
91 95 print("Block assigned to '%s'" % name)
92 96 else:
93 97 b = self.preclean_input(block)
94 98 self.shell.user_ns['pasted_block'] = b
95 99 self.shell.using_paste_magics = True
96 100 try:
97 101 self.shell.run_cell(b)
98 102 finally:
99 103 self.shell.using_paste_magics = False
100 104
101 105 def preclean_input(self, block):
102 106 lines = block.splitlines()
103 107 while lines and not lines[0].strip():
104 108 lines = lines[1:]
105 109 return strip_email_quotes('\n'.join(lines))
106 110
107 111 def rerun_pasted(self, name='pasted_block'):
108 112 """ Rerun a previously pasted command.
109 113 """
110 114 b = self.shell.user_ns.get(name)
111 115
112 116 # Sanity checks
113 117 if b is None:
114 118 raise UsageError('No previous pasted block available')
115 119 if not isinstance(b, py3compat.string_types):
116 120 raise UsageError(
117 121 "Variable 'pasted_block' is not a string, can't execute")
118 122
119 123 print("Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b)))
120 124 self.shell.run_cell(b)
121 125
122 126 @line_magic
123 127 def autoindent(self, parameter_s = ''):
124 128 """Toggle autoindent on/off (if available)."""
125 129
126 130 self.shell.set_autoindent()
127 131 print("Automatic indentation is:",['OFF','ON'][self.shell.autoindent])
128 132
129 133 @skip_doctest
130 134 @line_magic
131 135 def cpaste(self, parameter_s=''):
132 136 """Paste & execute a pre-formatted code block from clipboard.
133 137
134 138 You must terminate the block with '--' (two minus-signs) or Ctrl-D
135 139 alone on the line. You can also provide your own sentinel with '%paste
136 -s %%' ('%%' is the new sentinel for this operation)
140 -s %%' ('%%' is the new sentinel for this operation).
137 141
138 142 The block is dedented prior to execution to enable execution of method
139 143 definitions. '>' and '+' characters at the beginning of a line are
140 144 ignored, to allow pasting directly from e-mails, diff files and
141 145 doctests (the '...' continuation prompt is also stripped). The
142 146 executed block is also assigned to variable named 'pasted_block' for
143 147 later editing with '%edit pasted_block'.
144 148
145 149 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
146 150 This assigns the pasted block to variable 'foo' as string, without
147 151 dedenting or executing it (preceding >>> and + is still stripped)
148 152
149 153 '%cpaste -r' re-executes the block previously entered by cpaste.
154 '%cpaste -q' suppresses any additional output messages.
150 155
151 156 Do not be alarmed by garbled output on Windows (it's a readline bug).
152 157 Just press enter and type -- (and press enter again) and the block
153 158 will be what was just pasted.
154 159
155 160 IPython statements (magics, shell escapes) are not supported (yet).
156 161
157 162 See also
158 163 --------
159 164 paste: automatically pull code from clipboard.
160 165
161 166 Examples
162 167 --------
163 168 ::
164 169
165 170 In [8]: %cpaste
166 171 Pasting code; enter '--' alone on the line to stop.
167 172 :>>> a = ["world!", "Hello"]
168 173 :>>> print " ".join(sorted(a))
169 174 :--
170 175 Hello world!
171 176 """
172 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
177 opts, name = self.parse_options(parameter_s, 'rqs:', mode='string')
173 178 if 'r' in opts:
174 179 self.rerun_pasted()
175 180 return
176 181
182 quiet = ('q' in opts)
183
177 184 sentinel = opts.get('s', '--')
178 block = '\n'.join(get_pasted_lines(sentinel))
185 block = '\n'.join(get_pasted_lines(sentinel, quiet=quiet))
179 186 self.store_or_execute(block, name)
180 187
181 188 @line_magic
182 189 def paste(self, parameter_s=''):
183 190 """Paste & execute a pre-formatted code block from clipboard.
184 191
185 192 The text is pulled directly from the clipboard without user
186 193 intervention and printed back on the screen before execution (unless
187 194 the -q flag is given to force quiet mode).
188 195
189 196 The block is dedented prior to execution to enable execution of method
190 197 definitions. '>' and '+' characters at the beginning of a line are
191 198 ignored, to allow pasting directly from e-mails, diff files and
192 199 doctests (the '...' continuation prompt is also stripped). The
193 200 executed block is also assigned to variable named 'pasted_block' for
194 201 later editing with '%edit pasted_block'.
195 202
196 203 You can also pass a variable name as an argument, e.g. '%paste foo'.
197 204 This assigns the pasted block to variable 'foo' as string, without
198 205 executing it (preceding >>> and + is still stripped).
199 206
200 207 Options:
201 208
202 209 -r: re-executes the block previously entered by cpaste.
203 210
204 211 -q: quiet mode: do not echo the pasted text back to the terminal.
205 212
206 213 IPython statements (magics, shell escapes) are not supported (yet).
207 214
208 215 See also
209 216 --------
210 217 cpaste: manually paste code into terminal until you mark its end.
211 218 """
212 219 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
213 220 if 'r' in opts:
214 221 self.rerun_pasted()
215 222 return
216 223 try:
217 224 block = self.shell.hooks.clipboard_get()
218 225 except TryNext as clipboard_exc:
219 226 message = getattr(clipboard_exc, 'args')
220 227 if message:
221 228 error(message[0])
222 229 else:
223 230 error('Could not get text from the clipboard.')
224 231 return
225 232 except ClipboardEmpty:
226 233 raise UsageError("The clipboard appears to be empty")
227 234
228 235 # By default, echo back to terminal unless quiet mode is requested
229 236 if 'q' not in opts:
230 237 write = self.shell.write
231 238 write(self.shell.pycolorize(block))
232 239 if not block.endswith('\n'):
233 240 write('\n')
234 241 write("## -- End pasted text --\n")
235 242
236 243 self.store_or_execute(block, name)
237 244
238 245 # Class-level: add a '%cls' magic only on Windows
239 246 if sys.platform == 'win32':
240 247 @line_magic
241 248 def cls(self, s):
242 249 """Clear screen.
243 250 """
244 251 os.system("cls")
245 252
246 253 #-----------------------------------------------------------------------------
247 254 # Main class
248 255 #-----------------------------------------------------------------------------
249 256
250 257 class TerminalInteractiveShell(InteractiveShell):
251 258
252 259 autoedit_syntax = CBool(False, config=True,
253 260 help="auto editing of files with syntax errors.")
254 261 banner = Unicode('')
255 262 banner1 = Unicode(default_banner, config=True,
256 263 help="""The part of the banner to be printed before the profile"""
257 264 )
258 265 banner2 = Unicode('', config=True,
259 266 help="""The part of the banner to be printed after the profile"""
260 267 )
261 268 confirm_exit = CBool(True, config=True,
262 269 help="""
263 270 Set to confirm when you try to exit IPython with an EOF (Control-D
264 271 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
265 272 you can force a direct exit without any confirmation.""",
266 273 )
267 274 # This display_banner only controls whether or not self.show_banner()
268 275 # is called when mainloop/interact are called. The default is False
269 276 # because for the terminal based application, the banner behavior
270 277 # is controlled by Global.display_banner, which IPythonApp looks at
271 278 # to determine if *it* should call show_banner() by hand or not.
272 279 display_banner = CBool(False) # This isn't configurable!
273 280 embedded = CBool(False)
274 281 embedded_active = CBool(False)
275 282 editor = Unicode(get_default_editor(), config=True,
276 283 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
277 284 )
278 285 pager = Unicode('less', config=True,
279 286 help="The shell program to be used for paging.")
280 287
281 288 screen_length = Integer(0, config=True,
282 289 help=
283 290 """Number of lines of your screen, used to control printing of very
284 291 long strings. Strings longer than this number of lines will be sent
285 292 through a pager instead of directly printed. The default value for
286 293 this is 0, which means IPython will auto-detect your screen size every
287 294 time it needs to print certain potentially long strings (this doesn't
288 295 change the behavior of the 'print' keyword, it's only triggered
289 296 internally). If for some reason this isn't working well (it needs
290 297 curses support), specify it yourself. Otherwise don't change the
291 298 default.""",
292 299 )
293 300 term_title = CBool(False, config=True,
294 301 help="Enable auto setting the terminal title."
295 302 )
296 303
297 304 # This `using_paste_magics` is used to detect whether the code is being
298 305 # executed via paste magics functions
299 306 using_paste_magics = CBool(False)
300 307
301 308 # In the terminal, GUI control is done via PyOS_InputHook
302 309 @staticmethod
303 310 def enable_gui(gui=None, app=None):
304 311 """Switch amongst GUI input hooks by name.
305 312 """
306 313 # Deferred import
307 314 from IPython.lib.inputhook import enable_gui as real_enable_gui
308 315 try:
309 316 return real_enable_gui(gui, app)
310 317 except ValueError as e:
311 318 raise UsageError("%s" % e)
312 319
313 320 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
314 321 user_ns=None, user_module=None, custom_exceptions=((),None),
315 322 usage=None, banner1=None, banner2=None, display_banner=None,
316 323 **kwargs):
317 324
318 325 super(TerminalInteractiveShell, self).__init__(
319 326 config=config, ipython_dir=ipython_dir, profile_dir=profile_dir, user_ns=user_ns,
320 327 user_module=user_module, custom_exceptions=custom_exceptions,
321 328 **kwargs
322 329 )
323 330 # use os.system instead of utils.process.system by default,
324 331 # because piped system doesn't make sense in the Terminal:
325 332 self.system = self.system_raw
326 333
327 334 self.init_term_title()
328 335 self.init_usage(usage)
329 336 self.init_banner(banner1, banner2, display_banner)
330 337
331 338 #-------------------------------------------------------------------------
332 339 # Overrides of init stages
333 340 #-------------------------------------------------------------------------
334 341
335 342 def init_display_formatter(self):
336 343 super(TerminalInteractiveShell, self).init_display_formatter()
337 344 # terminal only supports plaintext
338 345 self.display_formatter.active_types = ['text/plain']
339 346
340 347 #-------------------------------------------------------------------------
341 348 # Things related to the terminal
342 349 #-------------------------------------------------------------------------
343 350
344 351 @property
345 352 def usable_screen_length(self):
346 353 if self.screen_length == 0:
347 354 return 0
348 355 else:
349 356 num_lines_bot = self.separate_in.count('\n')+1
350 357 return self.screen_length - num_lines_bot
351 358
352 359 def init_term_title(self):
353 360 # Enable or disable the terminal title.
354 361 if self.term_title:
355 362 toggle_set_term_title(True)
356 363 set_term_title('IPython: ' + abbrev_cwd())
357 364 else:
358 365 toggle_set_term_title(False)
359 366
360 367 #-------------------------------------------------------------------------
361 368 # Things related to aliases
362 369 #-------------------------------------------------------------------------
363 370
364 371 def init_alias(self):
365 372 # The parent class defines aliases that can be safely used with any
366 373 # frontend.
367 374 super(TerminalInteractiveShell, self).init_alias()
368 375
369 376 # Now define aliases that only make sense on the terminal, because they
370 377 # need direct access to the console in a way that we can't emulate in
371 378 # GUI or web frontend
372 379 if os.name == 'posix':
373 380 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
374 381 ('man', 'man')]
375 382 elif os.name == 'nt':
376 383 aliases = [('cls', 'cls')]
377 384
378 385
379 386 for name, cmd in aliases:
380 387 self.alias_manager.soft_define_alias(name, cmd)
381 388
382 389 #-------------------------------------------------------------------------
383 390 # Things related to the banner and usage
384 391 #-------------------------------------------------------------------------
385 392
386 393 def _banner1_changed(self):
387 394 self.compute_banner()
388 395
389 396 def _banner2_changed(self):
390 397 self.compute_banner()
391 398
392 399 def _term_title_changed(self, name, new_value):
393 400 self.init_term_title()
394 401
395 402 def init_banner(self, banner1, banner2, display_banner):
396 403 if banner1 is not None:
397 404 self.banner1 = banner1
398 405 if banner2 is not None:
399 406 self.banner2 = banner2
400 407 if display_banner is not None:
401 408 self.display_banner = display_banner
402 409 self.compute_banner()
403 410
404 411 def show_banner(self, banner=None):
405 412 if banner is None:
406 413 banner = self.banner
407 414 self.write(banner)
408 415
409 416 def compute_banner(self):
410 417 self.banner = self.banner1
411 418 if self.profile and self.profile != 'default':
412 419 self.banner += '\nIPython profile: %s\n' % self.profile
413 420 if self.banner2:
414 421 self.banner += '\n' + self.banner2
415 422
416 423 def init_usage(self, usage=None):
417 424 if usage is None:
418 425 self.usage = interactive_usage
419 426 else:
420 427 self.usage = usage
421 428
422 429 #-------------------------------------------------------------------------
423 430 # Mainloop and code execution logic
424 431 #-------------------------------------------------------------------------
425 432
426 433 def mainloop(self, display_banner=None):
427 434 """Start the mainloop.
428 435
429 436 If an optional banner argument is given, it will override the
430 437 internally created default banner.
431 438 """
432 439
433 440 with self.builtin_trap, self.display_trap:
434 441
435 442 while 1:
436 443 try:
437 444 self.interact(display_banner=display_banner)
438 445 #self.interact_with_readline()
439 446 # XXX for testing of a readline-decoupled repl loop, call
440 447 # interact_with_readline above
441 448 break
442 449 except KeyboardInterrupt:
443 450 # this should not be necessary, but KeyboardInterrupt
444 451 # handling seems rather unpredictable...
445 452 self.write("\nKeyboardInterrupt in interact()\n")
446 453
447 454 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
448 455 """Store multiple lines as a single entry in history"""
449 456
450 457 # do nothing without readline or disabled multiline
451 458 if not self.has_readline or not self.multiline_history:
452 459 return hlen_before_cell
453 460
454 461 # windows rl has no remove_history_item
455 462 if not hasattr(self.readline, "remove_history_item"):
456 463 return hlen_before_cell
457 464
458 465 # skip empty cells
459 466 if not source_raw.rstrip():
460 467 return hlen_before_cell
461 468
462 469 # nothing changed do nothing, e.g. when rl removes consecutive dups
463 470 hlen = self.readline.get_current_history_length()
464 471 if hlen == hlen_before_cell:
465 472 return hlen_before_cell
466 473
467 474 for i in range(hlen - hlen_before_cell):
468 475 self.readline.remove_history_item(hlen - i - 1)
469 476 stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
470 477 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
471 478 stdin_encoding))
472 479 return self.readline.get_current_history_length()
473 480
474 481 def interact(self, display_banner=None):
475 482 """Closely emulate the interactive Python console."""
476 483
477 484 # batch run -> do not interact
478 485 if self.exit_now:
479 486 return
480 487
481 488 if display_banner is None:
482 489 display_banner = self.display_banner
483 490
484 491 if isinstance(display_banner, py3compat.string_types):
485 492 self.show_banner(display_banner)
486 493 elif display_banner:
487 494 self.show_banner()
488 495
489 496 more = False
490 497
491 498 if self.has_readline:
492 499 self.readline_startup_hook(self.pre_readline)
493 500 hlen_b4_cell = self.readline.get_current_history_length()
494 501 else:
495 502 hlen_b4_cell = 0
496 503 # exit_now is set by a call to %Exit or %Quit, through the
497 504 # ask_exit callback.
498 505
499 506 while not self.exit_now:
500 507 self.hooks.pre_prompt_hook()
501 508 if more:
502 509 try:
503 510 prompt = self.prompt_manager.render('in2')
504 511 except:
505 512 self.showtraceback()
506 513 if self.autoindent:
507 514 self.rl_do_indent = True
508 515
509 516 else:
510 517 try:
511 518 prompt = self.separate_in + self.prompt_manager.render('in')
512 519 except:
513 520 self.showtraceback()
514 521 try:
515 522 line = self.raw_input(prompt)
516 523 if self.exit_now:
517 524 # quick exit on sys.std[in|out] close
518 525 break
519 526 if self.autoindent:
520 527 self.rl_do_indent = False
521 528
522 529 except KeyboardInterrupt:
523 530 #double-guard against keyboardinterrupts during kbdint handling
524 531 try:
525 532 self.write('\nKeyboardInterrupt\n')
526 533 source_raw = self.input_splitter.source_raw_reset()[1]
527 534 hlen_b4_cell = \
528 535 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
529 536 more = False
530 537 except KeyboardInterrupt:
531 538 pass
532 539 except EOFError:
533 540 if self.autoindent:
534 541 self.rl_do_indent = False
535 542 if self.has_readline:
536 543 self.readline_startup_hook(None)
537 544 self.write('\n')
538 545 self.exit()
539 546 except bdb.BdbQuit:
540 547 warn('The Python debugger has exited with a BdbQuit exception.\n'
541 548 'Because of how pdb handles the stack, it is impossible\n'
542 549 'for IPython to properly format this particular exception.\n'
543 550 'IPython will resume normal operation.')
544 551 except:
545 552 # exceptions here are VERY RARE, but they can be triggered
546 553 # asynchronously by signal handlers, for example.
547 554 self.showtraceback()
548 555 else:
549 556 self.input_splitter.push(line)
550 557 more = self.input_splitter.push_accepts_more()
551 558 if (self.SyntaxTB.last_syntax_error and
552 559 self.autoedit_syntax):
553 560 self.edit_syntax_error()
554 561 if not more:
555 562 source_raw = self.input_splitter.source_raw_reset()[1]
556 563 self.run_cell(source_raw, store_history=True)
557 564 hlen_b4_cell = \
558 565 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
559 566
560 567 # Turn off the exit flag, so the mainloop can be restarted if desired
561 568 self.exit_now = False
562 569
563 570 def raw_input(self, prompt=''):
564 571 """Write a prompt and read a line.
565 572
566 573 The returned line does not include the trailing newline.
567 574 When the user enters the EOF key sequence, EOFError is raised.
568 575
569 576 Parameters
570 577 ----------
571 578
572 579 prompt : str, optional
573 580 A string to be printed to prompt the user.
574 581 """
575 582 # raw_input expects str, but we pass it unicode sometimes
576 583 prompt = py3compat.cast_bytes_py2(prompt)
577 584
578 585 try:
579 586 line = py3compat.str_to_unicode(self.raw_input_original(prompt))
580 587 except ValueError:
581 588 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
582 589 " or sys.stdout.close()!\nExiting IPython!\n")
583 590 self.ask_exit()
584 591 return ""
585 592
586 593 # Try to be reasonably smart about not re-indenting pasted input more
587 594 # than necessary. We do this by trimming out the auto-indent initial
588 595 # spaces, if the user's actual input started itself with whitespace.
589 596 if self.autoindent:
590 597 if num_ini_spaces(line) > self.indent_current_nsp:
591 598 line = line[self.indent_current_nsp:]
592 599 self.indent_current_nsp = 0
593 600
594 601 return line
595 602
596 603 #-------------------------------------------------------------------------
597 604 # Methods to support auto-editing of SyntaxErrors.
598 605 #-------------------------------------------------------------------------
599 606
600 607 def edit_syntax_error(self):
601 608 """The bottom half of the syntax error handler called in the main loop.
602 609
603 610 Loop until syntax error is fixed or user cancels.
604 611 """
605 612
606 613 while self.SyntaxTB.last_syntax_error:
607 614 # copy and clear last_syntax_error
608 615 err = self.SyntaxTB.clear_err_state()
609 616 if not self._should_recompile(err):
610 617 return
611 618 try:
612 619 # may set last_syntax_error again if a SyntaxError is raised
613 620 self.safe_execfile(err.filename,self.user_ns)
614 621 except:
615 622 self.showtraceback()
616 623 else:
617 624 try:
618 625 f = open(err.filename)
619 626 try:
620 627 # This should be inside a display_trap block and I
621 628 # think it is.
622 629 sys.displayhook(f.read())
623 630 finally:
624 631 f.close()
625 632 except:
626 633 self.showtraceback()
627 634
628 635 def _should_recompile(self,e):
629 636 """Utility routine for edit_syntax_error"""
630 637
631 638 if e.filename in ('<ipython console>','<input>','<string>',
632 639 '<console>','<BackgroundJob compilation>',
633 640 None):
634 641
635 642 return False
636 643 try:
637 644 if (self.autoedit_syntax and
638 645 not self.ask_yes_no('Return to editor to correct syntax error? '
639 646 '[Y/n] ','y')):
640 647 return False
641 648 except EOFError:
642 649 return False
643 650
644 651 def int0(x):
645 652 try:
646 653 return int(x)
647 654 except TypeError:
648 655 return 0
649 656 # always pass integer line and offset values to editor hook
650 657 try:
651 658 self.hooks.fix_error_editor(e.filename,
652 659 int0(e.lineno),int0(e.offset),e.msg)
653 660 except TryNext:
654 661 warn('Could not open editor')
655 662 return False
656 663 return True
657 664
658 665 #-------------------------------------------------------------------------
659 666 # Things related to exiting
660 667 #-------------------------------------------------------------------------
661 668
662 669 def ask_exit(self):
663 670 """ Ask the shell to exit. Can be overiden and used as a callback. """
664 671 self.exit_now = True
665 672
666 673 def exit(self):
667 674 """Handle interactive exit.
668 675
669 676 This method calls the ask_exit callback."""
670 677 if self.confirm_exit:
671 678 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
672 679 self.ask_exit()
673 680 else:
674 681 self.ask_exit()
675 682
676 683 #-------------------------------------------------------------------------
677 684 # Things related to magics
678 685 #-------------------------------------------------------------------------
679 686
680 687 def init_magics(self):
681 688 super(TerminalInteractiveShell, self).init_magics()
682 689 self.register_magics(TerminalMagics)
683 690
684 691 def showindentationerror(self):
685 692 super(TerminalInteractiveShell, self).showindentationerror()
686 693 if not self.using_paste_magics:
687 694 print("If you want to paste code into IPython, try the "
688 695 "%paste and %cpaste magic functions.")
689 696
690 697
691 698 InteractiveShellABC.register(TerminalInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now