##// END OF EJS Templates
pdb support in threaded mode, replaced the crash handler with a verbose...
fperez -
Show More
@@ -1,7 +1,7 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 962 2005-12-28 18:04:59Z fperez $"""
4 $Id: Magic.py 965 2005-12-28 23:23:09Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1049,14 +1049,21 b' Currently the magic system has the following functions:\\n"""'
1049 try:
1049 try:
1050 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1050 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1051 except KeyError:
1051 except KeyError:
1052 print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.'
1052 print 'Incorrect argument. Use on/1, off/0, or nothing for a toggle.'
1053 return
1053 return
1054 else:
1054 else:
1055 self.shell.InteractiveTB.call_pdb = pdb
1055 self.shell.InteractiveTB.call_pdb = pdb
1056 else:
1056 else:
1057 self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb
1057 new_pdb = not self.shell.InteractiveTB.call_pdb
1058 print 'Automatic pdb calling has been turned',\
1058 self.shell.InteractiveTB.call_pdb = new_pdb
1059 on_off(self.shell.InteractiveTB.call_pdb)
1059 if self.shell.isthreaded:
1060 try:
1061 self.sys_excepthook.call_pdb = new_pdb
1062 except:
1063 warn('Failed to activate pdb for threaded exception handler')
1064
1065 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1066
1060
1067
1061
1068
1062 def magic_prun(self, parameter_s ='',user_mode=1,
1069 def magic_prun(self, parameter_s ='',user_mode=1,
@@ -1895,12 +1902,23 b' Currently the magic system has the following functions:\\n"""'
1895
1902
1896 If called without arguments, acts as a toggle."""
1903 If called without arguments, acts as a toggle."""
1897
1904
1905 def xmode_switch_err(name):
1906 warn('Error changing %s exception modes.\n%s' %
1907 (name,sys.exc_info()[1]))
1908
1898 new_mode = parameter_s.strip().capitalize()
1909 new_mode = parameter_s.strip().capitalize()
1899 try:
1910 try:
1900 self.InteractiveTB.set_mode(mode = new_mode)
1911 self.InteractiveTB.set_mode(mode=new_mode)
1901 print 'Exception reporting mode:',self.InteractiveTB.mode
1912 print 'Exception reporting mode:',self.InteractiveTB.mode
1902 except:
1913 except:
1903 warn('Error changing exception modes.\n' + str(sys.exc_info()[1]))
1914 xmode_switch_err('user')
1915
1916 # threaded shells use a special handler in sys.excepthook
1917 if self.isthreaded:
1918 try:
1919 self.shell.sys_excepthook.set_mode(mode=new_mode)
1920 except:
1921 xmode_switch_err('threaded')
1904
1922
1905 def magic_colors(self,parameter_s = ''):
1923 def magic_colors(self,parameter_s = ''):
1906 """Switch color scheme for prompts, info system and exception handlers.
1924 """Switch color scheme for prompts, info system and exception handlers.
@@ -1908,6 +1926,11 b' Currently the magic system has the following functions:\\n"""'
1908 Currently implemented schemes: NoColor, Linux, LightBG.
1926 Currently implemented schemes: NoColor, Linux, LightBG.
1909
1927
1910 Color scheme names are not case-sensitive."""
1928 Color scheme names are not case-sensitive."""
1929
1930 def color_switch_err(name):
1931 warn('Error changing %s color schemes.\n%s' %
1932 (name,sys.exc_info()[1]))
1933
1911
1934
1912 new_scheme = parameter_s.strip()
1935 new_scheme = parameter_s.strip()
1913 if not new_scheme:
1936 if not new_scheme:
@@ -1943,8 +1966,7 b' Defaulting color scheme to \'NoColor\'"""'
1943 try:
1966 try:
1944 self.shell.outputcache.set_colors(new_scheme)
1967 self.shell.outputcache.set_colors(new_scheme)
1945 except:
1968 except:
1946 warn('Error changing prompt color schemes.\n'
1969 color_switch_err('prompt')
1947 + str(sys.exc_info()[1]))
1948 else:
1970 else:
1949 self.shell.rc.colors = \
1971 self.shell.rc.colors = \
1950 self.shell.outputcache.color_table.active_scheme_name
1972 self.shell.outputcache.color_table.active_scheme_name
@@ -1953,15 +1975,21 b' Defaulting color scheme to \'NoColor\'"""'
1953 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1975 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1954 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1976 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1955 except:
1977 except:
1956 warn('Error changing exception color schemes.\n'
1978 color_switch_err('exception')
1957 + str(sys.exc_info()[1]))
1979
1980 # threaded shells use a verbose traceback in sys.excepthook
1981 if self.isthreaded:
1982 try:
1983 self.shell.sys_excepthook.set_colors(scheme=new_scheme)
1984 except:
1985 color_switch_err('system exception handler')
1986
1958 # Set info (for 'object?') colors
1987 # Set info (for 'object?') colors
1959 if self.shell.rc.color_info:
1988 if self.shell.rc.color_info:
1960 try:
1989 try:
1961 self.shell.inspector.set_active_scheme(new_scheme)
1990 self.shell.inspector.set_active_scheme(new_scheme)
1962 except:
1991 except:
1963 warn('Error changing object inspector color schemes.\n'
1992 color_switch_err('object inspector')
1964 + str(sys.exc_info()[1]))
1965 else:
1993 else:
1966 self.shell.inspector.set_active_scheme('NoColor')
1994 self.shell.inspector.set_active_scheme('NoColor')
1967
1995
@@ -4,7 +4,7 b''
4 All the matplotlib support code was co-developed with John Hunter,
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
5 matplotlib's author.
6
6
7 $Id: Shell.py 964 2005-12-28 21:03:01Z fperez $"""
7 $Id: Shell.py 965 2005-12-28 23:23:09Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
@@ -260,6 +260,10 b' class MTInteractiveShell(InteractiveShell):'
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
261 # from the pygtk mailing list, to avoid lockups with system calls.
261 # from the pygtk mailing list, to avoid lockups with system calls.
262
262
263 # class attribute to indicate whether the class supports threads or not.
264 # Subclasses with thread support should override this as needed.
265 isthreaded = True
266
263 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
264 user_ns=None,user_global_ns=None,banner2='',**kw):
268 user_ns=None,user_global_ns=None,banner2='',**kw):
265 """Similar to the normal InteractiveShell, but with threading control"""
269 """Similar to the normal InteractiveShell, but with threading control"""
@@ -6,7 +6,7 b' Requires Python 2.1 or newer.'
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 964 2005-12-28 21:03:01Z fperez $
9 $Id: iplib.py 965 2005-12-28 23:23:09Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -232,6 +232,10 b' class SyntaxTB(ultraTB.ListTB):'
232 class InteractiveShell(Logger, Magic):
232 class InteractiveShell(Logger, Magic):
233 """An enhanced console for Python."""
233 """An enhanced console for Python."""
234
234
235 # class attribute to indicate whether the class supports threads or not.
236 # Subclasses with thread support should override this as needed.
237 isthreaded = False
238
235 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
239 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
236 user_ns = None,user_global_ns=None,banner2='',
240 user_ns = None,user_global_ns=None,banner2='',
237 custom_exceptions=((),None),embedded=False):
241 custom_exceptions=((),None),embedded=False):
@@ -268,6 +272,9 b' class InteractiveShell(Logger, Magic):'
268 except AttributeError:
272 except AttributeError:
269 pass
273 pass
270
274
275 # Store the actual shell's name
276 self.name = name
277
271 # We need to know whether the instance is meant for embedding, since
278 # We need to know whether the instance is meant for embedding, since
272 # global/local namespaces need to be handled differently in that case
279 # global/local namespaces need to be handled differently in that case
273 self.embedded = embedded
280 self.embedded = embedded
@@ -405,9 +412,6 b' class InteractiveShell(Logger, Magic):'
405 self.user_ns['In'] = self.input_hist
412 self.user_ns['In'] = self.input_hist
406 self.user_ns['Out'] = self.output_hist
413 self.user_ns['Out'] = self.output_hist
407
414
408 # Store the actual shell's name
409 self.name = name
410
411 # Object variable to store code object waiting execution. This is
415 # Object variable to store code object waiting execution. This is
412 # used mainly by the multithreaded shells, but it can come in handy in
416 # used mainly by the multithreaded shells, but it can come in handy in
413 # other situations. No need to use a Queue here, since it's a single
417 # other situations. No need to use a Queue here, since it's a single
@@ -565,14 +569,35 b' class InteractiveShell(Logger, Magic):'
565 self.banner2 = banner2
569 self.banner2 = banner2
566
570
567 # TraceBack handlers:
571 # TraceBack handlers:
568 # Need two, one for syntax errors and one for other exceptions.
572
573 # Syntax error handler.
569 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
574 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
575
570 # The interactive one is initialized with an offset, meaning we always
576 # The interactive one is initialized with an offset, meaning we always
571 # want to remove the topmost item in the traceback, which is our own
577 # want to remove the topmost item in the traceback, which is our own
572 # internal code. Valid modes: ['Plain','Context','Verbose']
578 # internal code. Valid modes: ['Plain','Context','Verbose']
573 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
579 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
574 color_scheme='NoColor',
580 color_scheme='NoColor',
575 tb_offset = 1)
581 tb_offset = 1)
582
583 # IPython itself shouldn't crash. This will produce a detailed
584 # post-mortem if it does. But we only install the crash handler for
585 # non-threaded shells, the threaded ones use a normal verbose reporter
586 # and lose the crash handler. This is because exceptions in the main
587 # thread (such as in GUI code) propagate directly to sys.excepthook,
588 # and there's no point in printing crash dumps for every user exception.
589 if self.isthreaded:
590 sys.excepthook = ultraTB.FormattedTB()
591 else:
592 from IPython import CrashHandler
593 sys.excepthook = CrashHandler.CrashHandler(self)
594
595 # The instance will store a pointer to this, so that runtime code
596 # (such as magics) can access it. This is because during the
597 # read-eval loop, it gets temporarily overwritten (to deal with GUI
598 # frameworks).
599 self.sys_excepthook = sys.excepthook
600
576 # and add any custom exception handlers the user may have specified
601 # and add any custom exception handlers the user may have specified
577 self.set_custom_exc(*custom_exceptions)
602 self.set_custom_exc(*custom_exceptions)
578
603
@@ -618,6 +643,38 b' class InteractiveShell(Logger, Magic):'
618 self.init_auto_alias()
643 self.init_auto_alias()
619 # end __init__
644 # end __init__
620
645
646 def post_config_initialization(self):
647 """Post configuration init method
648
649 This is called after the configuration files have been processed to
650 'finalize' the initialization."""
651
652 rc = self.rc
653
654 # Load readline proper
655 if rc.readline:
656 self.init_readline()
657
658 # Set user colors (don't do it in the constructor above so that it
659 # doesn't crash if colors option is invalid)
660 self.magic_colors(rc.colors)
661
662 # Load user aliases
663 for alias in rc.alias:
664 self.magic_alias(alias)
665
666 # dynamic data that survives through sessions
667 # XXX make the filename a config option?
668 persist_base = 'persist'
669 if rc.profile:
670 persist_base += '_%s' % rc.profile
671 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
672
673 try:
674 self.persist = pickle.load(file(self.persist_fname))
675 except:
676 self.persist = {}
677
621 def set_hook(self,name,hook):
678 def set_hook(self,name,hook):
622 """set_hook(name,hook) -> sets an internal IPython hook.
679 """set_hook(name,hook) -> sets an internal IPython hook.
623
680
@@ -727,38 +784,6 b' class InteractiveShell(Logger, Magic):'
727 self.Completer.namespace = self.user_ns
784 self.Completer.namespace = self.user_ns
728 self.Completer.global_namespace = self.user_global_ns
785 self.Completer.global_namespace = self.user_global_ns
729
786
730 def post_config_initialization(self):
731 """Post configuration init method
732
733 This is called after the configuration files have been processed to
734 'finalize' the initialization."""
735
736 rc = self.rc
737
738 # Load readline proper
739 if rc.readline:
740 self.init_readline()
741
742 # Set user colors (don't do it in the constructor above so that it
743 # doesn't crash if colors option is invalid)
744 self.magic_colors(rc.colors)
745
746 # Load user aliases
747 for alias in rc.alias:
748 self.magic_alias(alias)
749
750 # dynamic data that survives through sessions
751 # XXX make the filename a config option?
752 persist_base = 'persist'
753 if rc.profile:
754 persist_base += '_%s' % rc.profile
755 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
756
757 try:
758 self.persist = pickle.load(file(self.persist_fname))
759 except:
760 self.persist = {}
761
762 def init_auto_alias(self):
787 def init_auto_alias(self):
763 """Define some aliases automatically.
788 """Define some aliases automatically.
764
789
@@ -1462,6 +1487,10 b' want to merge them back into the new files.""" % locals()'
1462 # Set our own excepthook in case the user code tries to call it
1487 # Set our own excepthook in case the user code tries to call it
1463 # directly, so that the IPython crash handler doesn't get triggered
1488 # directly, so that the IPython crash handler doesn't get triggered
1464 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1489 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1490
1491 # we save the original sys.excepthook in the instance, in case config
1492 # code (such as magics) needs access to it.
1493 self.sys_excepthook = old_excepthook
1465 outflag = 1 # happens in more places, so it's easier as default
1494 outflag = 1 # happens in more places, so it's easier as default
1466 try:
1495 try:
1467 try:
1496 try:
@@ -1954,7 +1983,7 b' want to merge them back into the new files.""" % locals()'
1954 try:
1983 try:
1955 execfile(fname,*where)
1984 execfile(fname,*where)
1956 except SyntaxError:
1985 except SyntaxError:
1957 etype, evalue = sys.exc_info()[0:2]
1986 etype,evalue = sys.exc_info()[:2]
1958 self.SyntaxTB(etype,evalue,[])
1987 self.SyntaxTB(etype,evalue,[])
1959 warn('Failure executing file: <%s>' % fname)
1988 warn('Failure executing file: <%s>' % fname)
1960 except SystemExit,status:
1989 except SystemExit,status:
@@ -6,7 +6,7 b' Requires Python 2.1 or better.'
6
6
7 This file contains the main make_IPython() starter function.
7 This file contains the main make_IPython() starter function.
8
8
9 $Id: ipmaker.py 964 2005-12-28 21:03:01Z fperez $"""
9 $Id: ipmaker.py 965 2005-12-28 23:23:09Z fperez $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -94,15 +94,11 b' def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,'
94 from site import _Helper
94 from site import _Helper
95 IP.user_ns['help'] = _Helper()
95 IP.user_ns['help'] = _Helper()
96
96
97
97 if DEVDEBUG:
98 if DEVDEBUG:
98 # For developer debugging only (global flag)
99 # For developer debugging only (global flag)
99 from IPython import ultraTB
100 from IPython import ultraTB
100 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101 else:
102 # IPython itself shouldn't crash. This will produce a detailed
103 # post-mortem if it does
104 from IPython import CrashHandler
105 sys.excepthook = CrashHandler.CrashHandler(IP)
106
102
107 IP.BANNER_PARTS = ['Python %s\n'
103 IP.BANNER_PARTS = ['Python %s\n'
108 'Type "copyright", "credits" or "license" '
104 'Type "copyright", "credits" or "license" '
@@ -60,7 +60,7 b' You can implement other color schemes easily, the syntax is fairly'
60 self-explanatory. Please send back new schemes you develop to the author for
60 self-explanatory. Please send back new schemes you develop to the author for
61 possible inclusion in future releases.
61 possible inclusion in future releases.
62
62
63 $Id: ultraTB.py 958 2005-12-27 23:17:51Z fperez $"""
63 $Id: ultraTB.py 965 2005-12-28 23:23:09Z fperez $"""
64
64
65 #*****************************************************************************
65 #*****************************************************************************
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
@@ -621,10 +621,10 b' class VerboseTB(TBTools):'
621 # out the right info on its own.
621 # out the right info on its own.
622 def __call__(self, etype=None, evalue=None, etb=None):
622 def __call__(self, etype=None, evalue=None, etb=None):
623 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
623 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
624 if etb is not None:
624 if etb is None:
625 self.handler((etype, evalue, etb))
626 else:
627 self.handler()
625 self.handler()
626 else:
627 self.handler((etype, evalue, etb))
628 self.debugger()
628 self.debugger()
629
629
630 #----------------------------------------------------------------------------
630 #----------------------------------------------------------------------------
@@ -5,8 +5,20 b''
5 questions part 2 - \/ characters revisited' on the iypthon user
5 questions part 2 - \/ characters revisited' on the iypthon user
6 list:
6 list:
7 http://scipy.net/pipermail/ipython-user/2005-June/000907.html
7 http://scipy.net/pipermail/ipython-user/2005-June/000907.html
8
8 (InteractiveShell.__init__): fix tab-completion bug in threaded shells.
9 (InteractiveShell.__init__): fix tab-completion bug in threaded shells.
9
10
11 (InteractiveShell.__init__): change threaded shells to not use the
12 ipython crash handler. This was causing more problems than not,
13 as exceptions in the main thread (GUI code, typically) would
14 always show up as a 'crash', when they really weren't.
15
16 The colors and exception mode commands (%colors/%xmode) have been
17 synchronized to also take this into account, so users can get
18 verbose exceptions for their threaded code as well. I also added
19 support for activating pdb inside this exception handler as well,
20 so now GUI authors can use IPython's enhanced pdb at runtime.
21
10 * IPython/ipmaker.py (make_IPython): make the autoedit_syntax flag
22 * IPython/ipmaker.py (make_IPython): make the autoedit_syntax flag
11 true by default, and add it to the shipped ipythonrc file. Since
23 true by default, and add it to the shipped ipythonrc file. Since
12 this asks the user before proceeding, I think it's OK to make it
24 this asks the user before proceeding, I think it's OK to make it
General Comments 0
You need to be logged in to leave comments. Login now