##// END OF EJS Templates
- add support for controlling the pylab threading mode directly at the...
fperez -
Show More
@@ -0,0 +1,94 b''
1 #!/usr/bin/env python
2 """A few unit tests for the Shell module.
3 """
4
5 from unittest import TestCase, main
6
7 from IPython import Shell
8
9 try:
10 import matplotlib
11 has_matplotlib = True
12 except ImportError:
13 has_matplotlib = False
14
15 class ShellTestBase(TestCase):
16 def _test(self,argv,ans):
17 shell = Shell._select_shell(argv)
18 err = 'Got %s != %s' % (shell,ans)
19 self.failUnlessEqual(shell,ans,err)
20
21 class ArgsTestCase(ShellTestBase):
22 def test_plain(self):
23 self._test([],Shell.IPShell)
24
25 def test_tkthread(self):
26 self._test(['-tkthread'],Shell.IPShell)
27
28 def test_gthread(self):
29 self._test(['-gthread'],Shell.IPShellGTK)
30
31 def test_qthread(self):
32 self._test(['-qthread'],Shell.IPShellQt)
33
34 def test_q4thread(self):
35 self._test(['-q4thread'],Shell.IPShellQt4)
36
37 def test_wthread(self):
38 self._test(['-wthread'],Shell.IPShellWX)
39
40 if has_matplotlib:
41 class MplArgsTestCase(ShellTestBase):
42 def setUp(self):
43 self.backend = matplotlib.rcParams['backend']
44
45 def tearDown(self):
46 matplotlib.rcParams['backend'] = self.backend
47
48 def _test(self,argv,ans):
49 shell = Shell._select_shell(argv)
50 err = 'Got %s != %s' % (shell,ans)
51 self.failUnlessEqual(shell,ans,err)
52
53 def test_tk(self):
54 matplotlib.rcParams['backend'] = 'TkAgg'
55 self._test(['-pylab'],Shell.IPShellMatplotlib)
56
57 def test_ps(self):
58 matplotlib.rcParams['backend'] = 'PS'
59 self._test(['-pylab'],Shell.IPShellMatplotlib)
60
61 def test_gtk(self):
62 matplotlib.rcParams['backend'] = 'GTKAgg'
63 self._test(['-pylab'],Shell.IPShellMatplotlibGTK)
64
65 def test_gtk_2(self):
66 self._test(['-gthread','-pylab'],Shell.IPShellMatplotlibGTK)
67 self.failUnlessEqual(matplotlib.rcParams['backend'],'GTKAgg')
68
69 def test_qt(self):
70 matplotlib.rcParams['backend'] = 'QtAgg'
71 self._test(['-pylab'],Shell.IPShellMatplotlibQt)
72
73 def test_qt_2(self):
74 self._test(['-qthread','-pylab'],Shell.IPShellMatplotlibQt)
75 self.failUnlessEqual(matplotlib.rcParams['backend'],'QtAgg')
76
77 def test_qt4(self):
78 matplotlib.rcParams['backend'] = 'Qt4Agg'
79 self._test(['-pylab'],Shell.IPShellMatplotlibQt4)
80
81 def test_qt4_2(self):
82 self._test(['-q4thread','-pylab'],Shell.IPShellMatplotlibQt4)
83 self.failUnlessEqual(matplotlib.rcParams['backend'],'Qt4Agg')
84
85 def test_wx(self):
86 matplotlib.rcParams['backend'] = 'WxAgg'
87 self._test(['-pylab'],Shell.IPShellMatplotlibWX)
88
89 def test_wx_2(self):
90 self._test(['-pylab','-wthread'],Shell.IPShellMatplotlibWX)
91 self.failUnlessEqual(matplotlib.rcParams['backend'],'WXAgg')
92
93
94 main()
@@ -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 2764 2007-09-15 14:16:21Z darren.dale $"""
7 $Id: Shell.py 2887 2007-12-12 08:28:43Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -1092,31 +1092,85 b' class IPShellMatplotlibQt4(IPShellQt4):'
1092 #-----------------------------------------------------------------------------
1092 #-----------------------------------------------------------------------------
1093 # Factory functions to actually start the proper thread-aware shell
1093 # Factory functions to actually start the proper thread-aware shell
1094
1094
1095 def _matplotlib_shell_class():
1095 def _select_shell(argv):
1096 """Factory function to handle shell class selection for matplotlib.
1096 """Select a shell from the given argv vector.
1097
1097
1098 The proper shell class to use depends on the matplotlib backend, since
1098 This function implements the threading selection policy, allowing runtime
1099 each backend requires a different threading strategy."""
1099 control of the threading mode, both for general users and for matplotlib.
1100
1100
1101 try:
1101 Return:
1102 import matplotlib
1102 Shell class to be instantiated for runtime operation.
1103 except ImportError:
1103 """
1104 error('matplotlib could NOT be imported! Starting normal IPython.')
1104
1105 sh_class = IPShell
1105 global USE_TK
1106 else:
1106
1107 backend = matplotlib.rcParams['backend']
1107 mpl_shell = {'gthread' : IPShellMatplotlibGTK,
1108 if backend.startswith('GTK'):
1108 'wthread' : IPShellMatplotlibWX,
1109 sh_class = IPShellMatplotlibGTK
1109 'qthread' : IPShellMatplotlibQt,
1110 elif backend.startswith('WX'):
1110 'q4thread' : IPShellMatplotlibQt4,
1111 sh_class = IPShellMatplotlibWX
1111 'tkthread' : IPShellMatplotlib, # Tk is built-in
1112 elif backend.startswith('Qt4'):
1112 }
1113 sh_class = IPShellMatplotlibQt4
1113
1114 elif backend.startswith('Qt'):
1114 th_shell = {'gthread' : IPShellGTK,
1115 sh_class = IPShellMatplotlibQt
1115 'wthread' : IPShellWX,
1116 'qthread' : IPShellQt,
1117 'q4thread' : IPShellQt4,
1118 'tkthread' : IPShell, # Tk is built-in
1119 }
1120
1121 backends = {'gthread' : 'GTKAgg',
1122 'wthread' : 'WXAgg',
1123 'qthread' : 'QtAgg',
1124 'q4thread' :'Qt4Agg',
1125 'tkthread' :'TkAgg',
1126 }
1127
1128 all_opts = set(['tk','pylab','gthread','qthread','q4thread','wthread',
1129 'tkthread'])
1130 user_opts = set([s.replace('-','') for s in argv[:3]])
1131 special_opts = user_opts & all_opts
1132
1133 if 'tk' in special_opts:
1134 USE_TK = True
1135 special_opts.remove('tk')
1136
1137 if 'pylab' in special_opts:
1138
1139 try:
1140 import matplotlib
1141 except ImportError:
1142 error('matplotlib could NOT be imported! Starting normal IPython.')
1143 return IPShell
1144
1145 special_opts.remove('pylab')
1146 # If there's any option left, it means the user wants to force the
1147 # threading backend, else it's auto-selected from the rc file
1148 if special_opts:
1149 th_mode = special_opts.pop()
1150 matplotlib.rcParams['backend'] = backends[th_mode]
1116 else:
1151 else:
1117 sh_class = IPShellMatplotlib
1152 backend = matplotlib.rcParams['backend']
1118 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
1153 if backend.startswith('GTK'):
1119 return sh_class
1154 th_mode = 'gthread'
1155 elif backend.startswith('WX'):
1156 th_mode = 'wthread'
1157 elif backend.startswith('Qt4'):
1158 th_mode = 'q4thread'
1159 elif backend.startswith('Qt'):
1160 th_mode = 'qthread'
1161 else:
1162 # Any other backend, use plain Tk
1163 th_mode = 'tkthread'
1164
1165 return mpl_shell[th_mode]
1166 else:
1167 # No pylab requested, just plain threads
1168 try:
1169 th_mode = special_opts.pop()
1170 except KeyError:
1171 th_mode = 'tkthread'
1172 return th_shell[th_mode]
1173
1120
1174
1121 # This is the one which should be called by external code.
1175 # This is the one which should be called by external code.
1122 def start(user_ns = None):
1176 def start(user_ns = None):
@@ -1126,29 +1180,7 b' def start(user_ns = None):'
1126 based on the user's threading choice. Such a selector is needed because
1180 based on the user's threading choice. Such a selector is needed because
1127 different GUI toolkits require different thread handling details."""
1181 different GUI toolkits require different thread handling details."""
1128
1182
1129 global USE_TK
1183 shell = _select_shell(sys.argv)
1130 # Crude sys.argv hack to extract the threading options.
1131 argv = sys.argv
1132 if len(argv) > 1:
1133 if len(argv) > 2:
1134 arg2 = argv[2]
1135 if arg2.endswith('-tk'):
1136 USE_TK = True
1137 arg1 = argv[1]
1138 if arg1.endswith('-gthread'):
1139 shell = IPShellGTK
1140 elif arg1.endswith( '-qthread' ):
1141 shell = IPShellQt
1142 elif arg1.endswith( '-q4thread' ):
1143 shell = IPShellQt4
1144 elif arg1.endswith('-wthread'):
1145 shell = IPShellWX
1146 elif arg1.endswith('-pylab'):
1147 shell = _matplotlib_shell_class()
1148 else:
1149 shell = IPShell
1150 else:
1151 shell = IPShell
1152 return shell(user_ns = user_ns)
1184 return shell(user_ns = user_ns)
1153
1185
1154 # Some aliases for backwards compatibility
1186 # Some aliases for backwards compatibility
@@ -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 2873 2007-11-25 18:01:55Z fperez $"""
9 $Id: ipmaker.py 2887 2007-12-12 08:28:43Z fperez $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -178,7 +178,7 b" object? -> Details about 'object'. ?object also works, ?? prints more."
178 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
178 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
179 # the 'C-c !' command in emacs automatically appends a -i option at the end.
179 # the 'C-c !' command in emacs automatically appends a -i option at the end.
180 cmdline_only = ('help interact|i ipythondir=s Version upgrade '
180 cmdline_only = ('help interact|i ipythondir=s Version upgrade '
181 'gthread! qthread! q4thread! wthread! pylab! tk!')
181 'gthread! qthread! q4thread! wthread! tkthread! pylab! tk!')
182
182
183 # Build the actual name list to be used by DPyGetOpt
183 # Build the actual name list to be used by DPyGetOpt
184 opts_names = qw(cmdline_opts) + qw(cmdline_only)
184 opts_names = qw(cmdline_opts) + qw(cmdline_only)
@@ -1,3 +1,10 b''
1 2007-12-12 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Shell.py (_select_shell): add support for controlling
4 the pylab threading mode directly at the command line, without
5 having to modify MPL config files. Added unit tests for this
6 feature, though manual/docs update is still pending, will do later.
7
1 2007-12-01 Robert Kern <robert.kern@gmail.com>
8 2007-12-01 Robert Kern <robert.kern@gmail.com>
2
9
3 * IPython/ultraTB.py (findsource): Improve the monkeypatch to
10 * IPython/ultraTB.py (findsource): Improve the monkeypatch to
General Comments 0
You need to be logged in to leave comments. Login now