##// END OF EJS Templates
Merge pull request #9373 from takluyver/ptshell-copy-options...
Matthias Bussonnier -
r22206:3fc7d39c merge
parent child Browse files
Show More
@@ -1432,6 +1432,7 b' class SyntaxTB(ListTB):'
1432 newtext = ulinecache.getline(value.filename, value.lineno)
1432 newtext = ulinecache.getline(value.filename, value.lineno)
1433 if newtext:
1433 if newtext:
1434 value.text = newtext
1434 value.text = newtext
1435 self.last_syntax_error = value
1435 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1436 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1436 tb_offset=tb_offset, context=context)
1437 tb_offset=tb_offset, context=context)
1437
1438
@@ -4,7 +4,9 b' from __future__ import print_function'
4 import os
4 import os
5 import sys
5 import sys
6 import signal
6 import signal
7 from warnings import warn
7
8
9 from IPython.core.error import TryNext
8 from IPython.core.interactiveshell import InteractiveShell
10 from IPython.core.interactiveshell import InteractiveShell
9 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
11 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
10 from IPython.utils.terminal import toggle_set_term_title, set_term_title
12 from IPython.utils.terminal import toggle_set_term_title, set_term_title
@@ -72,6 +74,9 b' class TerminalInteractiveShell(InteractiveShell):'
72
74
73 pt_cli = None
75 pt_cli = None
74
76
77 autoedit_syntax = CBool(False, config=True,
78 help="auto editing of files with syntax errors.")
79
75 confirm_exit = CBool(True, config=True,
80 confirm_exit = CBool(True, config=True,
76 help="""
81 help="""
77 Set to confirm when you try to exit IPython with an EOF (Control-D
82 Set to confirm when you try to exit IPython with an EOF (Control-D
@@ -295,6 +300,8 b' class TerminalInteractiveShell(InteractiveShell):'
295 else:
300 else:
296 if code:
301 if code:
297 self.run_cell(code, store_history=True)
302 self.run_cell(code, store_history=True)
303 if self.autoedit_syntax and self.SyntaxTB.last_syntax_error:
304 self.edit_syntax_error()
298
305
299 def mainloop(self):
306 def mainloop(self):
300 # An extra layer of protection in case someone mashing Ctrl-C breaks
307 # An extra layer of protection in case someone mashing Ctrl-C breaks
@@ -317,5 +324,64 b' class TerminalInteractiveShell(InteractiveShell):'
317 else:
324 else:
318 self._inputhook = None
325 self._inputhook = None
319
326
327 # Methods to support auto-editing of SyntaxErrors:
328
329 def edit_syntax_error(self):
330 """The bottom half of the syntax error handler called in the main loop.
331
332 Loop until syntax error is fixed or user cancels.
333 """
334
335 while self.SyntaxTB.last_syntax_error:
336 # copy and clear last_syntax_error
337 err = self.SyntaxTB.clear_err_state()
338 if not self._should_recompile(err):
339 return
340 try:
341 # may set last_syntax_error again if a SyntaxError is raised
342 self.safe_execfile(err.filename, self.user_ns)
343 except:
344 self.showtraceback()
345 else:
346 try:
347 with open(err.filename) as f:
348 # This should be inside a display_trap block and I
349 # think it is.
350 sys.displayhook(f.read())
351 except:
352 self.showtraceback()
353
354 def _should_recompile(self, e):
355 """Utility routine for edit_syntax_error"""
356
357 if e.filename in ('<ipython console>', '<input>', '<string>',
358 '<console>', '<BackgroundJob compilation>',
359 None):
360 return False
361 try:
362 if (self.autoedit_syntax and
363 not self.ask_yes_no(
364 'Return to editor to correct syntax error? '
365 '[Y/n] ', 'y')):
366 return False
367 except EOFError:
368 return False
369
370 def int0(x):
371 try:
372 return int(x)
373 except TypeError:
374 return 0
375
376 # always pass integer line and offset values to editor hook
377 try:
378 self.hooks.fix_error_editor(e.filename,
379 int0(e.lineno), int0(e.offset),
380 e.msg)
381 except TryNext:
382 warn('Could not open editor')
383 return False
384 return True
385
320 if __name__ == '__main__':
386 if __name__ == '__main__':
321 TerminalInteractiveShell.instance().interact()
387 TerminalInteractiveShell.instance().interact()
General Comments 0
You need to be logged in to leave comments. Login now