1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
|
# -*- coding: utf-8 -*-
"""IPython Shell classes.
All the matplotlib support code was co-developed with John Hunter,
matplotlib's author.
"""
#*****************************************************************************
# Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
# Code begins
# Stdlib imports
import __builtin__
import __main__
import Queue
import inspect
import os
import sys
import thread
import threading
import time
from signal import signal, SIGINT
try:
import ctypes
HAS_CTYPES = True
except ImportError:
HAS_CTYPES = False
# IPython imports
import IPython
from IPython import ultraTB, ipapi
from IPython.Magic import Magic
from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
from IPython.iplib import InteractiveShell
from IPython.ipmaker import make_IPython
from IPython.ipstruct import Struct
from IPython.testing import decorators as testdec
# Globals
# global flag to pass around information about Ctrl-C without exceptions
KBINT = False
# global flag to turn on/off Tk support.
USE_TK = False
# ID for the main thread, used for cross-thread exceptions
MAIN_THREAD_ID = thread.get_ident()
# Tag when runcode() is active, for exception handling
CODE_RUN = None
# Default timeout for waiting for multithreaded shells (in seconds)
GUI_TIMEOUT = 10
#-----------------------------------------------------------------------------
# This class is trivial now, but I want to have it in to publish a clean
# interface. Later when the internals are reorganized, code that uses this
# shouldn't have to change.
class IPShell:
"""Create an IPython instance."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,
debug=1,shell_class=InteractiveShell):
self.IP = make_IPython(argv,user_ns=user_ns,
user_global_ns=user_global_ns,
debug=debug,shell_class=shell_class)
def mainloop(self,sys_exit=0,banner=None):
self.IP.mainloop(banner)
if sys_exit:
sys.exit()
#-----------------------------------------------------------------------------
def kill_embedded(self,parameter_s=''):
"""%kill_embedded : deactivate for good the current embedded IPython.
This function (after asking for confirmation) sets an internal flag so that
an embedded IPython will never activate again. This is useful to
permanently disable a shell that is being called inside a loop: once you've
figured out what you needed from it, you may then kill it and the program
will then continue to run without the interactive shell interfering again.
"""
kill = ask_yes_no("Are you sure you want to kill this embedded instance "
"(y/n)? [y/N] ",'n')
if kill:
self.shell.embedded_active = False
print "This embedded IPython will not reactivate anymore once you exit."
class IPShellEmbed:
"""Allow embedding an IPython shell into a running program.
Instances of this class are callable, with the __call__ method being an
alias to the embed() method of an InteractiveShell instance.
Usage (see also the example-embed.py file for a running example):
ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
- argv: list containing valid command-line options for IPython, as they
would appear in sys.argv[1:].
For example, the following command-line options:
$ ipython -prompt_in1 'Input <\\#>' -colors LightBG
would be passed in the argv list as:
['-prompt_in1','Input <\\#>','-colors','LightBG']
- banner: string which gets printed every time the interpreter starts.
- exit_msg: string which gets printed every time the interpreter exits.
- rc_override: a dict or Struct of configuration options such as those
used by IPython. These options are read from your ~/.ipython/ipythonrc
file when the Shell object is created. Passing an explicit rc_override
dict with any options you want allows you to override those values at
creation time without having to modify the file. This way you can create
embeddable instances configured in any way you want without editing any
global files (thus keeping your interactive IPython configuration
unchanged).
Then the ipshell instance can be called anywhere inside your code:
ipshell(header='') -> Opens up an IPython shell.
- header: string printed by the IPython shell upon startup. This can let
you know where in your code you are when dropping into the shell. Note
that 'banner' gets prepended to all calls, so header is used for
location-specific information.
For more details, see the __call__ method below.
When the IPython shell is exited with Ctrl-D, normal program execution
resumes.
This functionality was inspired by a posting on comp.lang.python by cmkl
<cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
by the IDL stop/continue commands."""
def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
user_ns=None):
"""Note that argv here is a string, NOT a list."""
self.set_banner(banner)
self.set_exit_msg(exit_msg)
self.set_dummy_mode(0)
# sys.displayhook is a global, we need to save the user's original
# Don't rely on __displayhook__, as the user may have changed that.
self.sys_displayhook_ori = sys.displayhook
# save readline completer status
try:
#print 'Save completer',sys.ipcompleter # dbg
self.sys_ipcompleter_ori = sys.ipcompleter
except:
pass # not nested with IPython
self.IP = make_IPython(argv,rc_override=rc_override,
embedded=True,
user_ns=user_ns)
ip = ipapi.IPApi(self.IP)
ip.expose_magic("kill_embedded",kill_embedded)
# copy our own displayhook also
self.sys_displayhook_embed = sys.displayhook
# and leave the system's display hook clean
sys.displayhook = self.sys_displayhook_ori
# don't use the ipython crash handler so that user exceptions aren't
# trapped
sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
mode = self.IP.rc.xmode,
call_pdb = self.IP.rc.pdb)
self.restore_system_completer()
def restore_system_completer(self):
"""Restores the readline completer which was in place.
This allows embedded IPython within IPython not to disrupt the
parent's completion.
"""
try:
self.IP.readline.set_completer(self.sys_ipcompleter_ori)
sys.ipcompleter = self.sys_ipcompleter_ori
except:
pass
def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
"""Activate the interactive interpreter.
__call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
the interpreter shell with the given local and global namespaces, and
optionally print a header string at startup.
The shell can be globally activated/deactivated using the
set/get_dummy_mode methods. This allows you to turn off a shell used
for debugging globally.
However, *each* time you call the shell you can override the current
state of dummy_mode with the optional keyword parameter 'dummy'. For
example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
can still have a specific call work by making it as IPShell(dummy=0).
The optional keyword parameter dummy controls whether the call
actually does anything. """
# If the user has turned it off, go away
if not self.IP.embedded_active:
return
# Normal exits from interactive mode set this flag, so the shell can't
# re-enter (it checks this variable at the start of interactive mode).
self.IP.exit_now = False
# Allow the dummy parameter to override the global __dummy_mode
if dummy or (dummy != 0 and self.__dummy_mode):
return
# Set global subsystems (display,completions) to our values
sys.displayhook = self.sys_displayhook_embed
if self.IP.has_readline:
self.IP.set_completer()
if self.banner and header:
format = '%s\n%s\n'
else:
format = '%s%s\n'
banner = format % (self.banner,header)
# Call the embedding code with a stack depth of 1 so it can skip over
# our call and get the original caller's namespaces.
self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
if self.exit_msg:
print self.exit_msg
# Restore global systems (display, completion)
sys.displayhook = self.sys_displayhook_ori
self.restore_system_completer()
def set_dummy_mode(self,dummy):
"""Sets the embeddable shell's dummy mode parameter.
set_dummy_mode(dummy): dummy = 0 or 1.
This parameter is persistent and makes calls to the embeddable shell
silently return without performing any action. This allows you to
globally activate or deactivate a shell you're using with a single call.
If you need to manually"""
if dummy not in [0,1,False,True]:
raise ValueError,'dummy parameter must be boolean'
self.__dummy_mode = dummy
def get_dummy_mode(self):
"""Return the current value of the dummy mode parameter.
"""
return self.__dummy_mode
def set_banner(self,banner):
"""Sets the global banner.
This banner gets prepended to every header printed when the shell
instance is called."""
self.banner = banner
def set_exit_msg(self,exit_msg):
"""Sets the global exit_msg.
This exit message gets printed upon exiting every time the embedded
shell is called. It is None by default. """
self.exit_msg = exit_msg
#-----------------------------------------------------------------------------
if HAS_CTYPES:
# Add async exception support. Trick taken from:
# http://sebulba.wikispaces.com/recipe+thread2
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
if not inspect.isclass(exctype):
raise TypeError("Only types can be raised (not instances)")
# Explicit cast to c_long is necessary for 64-bit support:
# See https://bugs.launchpad.net/ipython/+bug/237073
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid),
ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# If it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
raise SystemError("PyThreadState_SetAsyncExc failed")
def sigint_handler(signum,stack_frame):
"""Sigint handler for threaded apps.
This is a horrible hack to pass information about SIGINT _without_
using exceptions, since I haven't been able to properly manage
cross-thread exceptions in GTK/WX. In fact, I don't think it can be
done (or at least that's my understanding from a c.l.py thread where
this was discussed)."""
global KBINT
if CODE_RUN:
_async_raise(MAIN_THREAD_ID,KeyboardInterrupt)
else:
KBINT = True
print '\nKeyboardInterrupt - Press <Enter> to continue.',
Term.cout.flush()
else:
def sigint_handler(signum,stack_frame):
"""Sigint handler for threaded apps.
This is a horrible hack to pass information about SIGINT _without_
using exceptions, since I haven't been able to properly manage
cross-thread exceptions in GTK/WX. In fact, I don't think it can be
done (or at least that's my understanding from a c.l.py thread where
this was discussed)."""
global KBINT
print '\nKeyboardInterrupt - Press <Enter> to continue.',
Term.cout.flush()
# Set global flag so that runsource can know that Ctrl-C was hit
KBINT = True
class MTInteractiveShell(InteractiveShell):
"""Simple multi-threaded shell."""
# Threading strategy taken from:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
# McErlean and John Finlay. Modified with corrections by Antoon Pardon,
# from the pygtk mailing list, to avoid lockups with system calls.
# class attribute to indicate whether the class supports threads or not.
# Subclasses with thread support should override this as needed.
isthreaded = True
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
user_ns=None,user_global_ns=None,banner2='',
gui_timeout=GUI_TIMEOUT,**kw):
"""Similar to the normal InteractiveShell, but with threading control"""
InteractiveShell.__init__(self,name,usage,rc,user_ns,
user_global_ns,banner2)
# Timeout we wait for GUI thread
self.gui_timeout = gui_timeout
# A queue to hold the code to be executed.
self.code_queue = Queue.Queue()
# Stuff to do at closing time
self._kill = None
on_kill = kw.get('on_kill', [])
# Check that all things to kill are callable:
for t in on_kill:
if not callable(t):
raise TypeError,'on_kill must be a list of callables'
self.on_kill = on_kill
# thread identity of the "worker thread" (that may execute code directly)
self.worker_ident = None
def runsource(self, source, filename="<input>", symbol="single"):
"""Compile and run some source in the interpreter.
Modified version of code.py's runsource(), to handle threading issues.
See the original for full docstring details."""
global KBINT
# If Ctrl-C was typed, we reset the flag and return right away
if KBINT:
KBINT = False
return False
if self._kill:
# can't queue new code if we are being killed
return True
try:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
self.showsyntaxerror(filename)
return False
if code is None:
# Case 2
return True
# shortcut - if we are in worker thread, or the worker thread is not
# running, execute directly (to allow recursion and prevent deadlock if
# code is run early in IPython construction)
if (self.worker_ident is None
or self.worker_ident == thread.get_ident() ):
InteractiveShell.runcode(self,code)
return False
# Case 3
# Store code in queue, so the execution thread can handle it.
completed_ev, received_ev = threading.Event(), threading.Event()
self.code_queue.put((code,completed_ev, received_ev))
# first make sure the message was received, with timeout
received_ev.wait(self.gui_timeout)
if not received_ev.isSet():
# the mainloop is dead, start executing code directly
print "Warning: Timeout for mainloop thread exceeded"
print "switching to nonthreaded mode (until mainloop wakes up again)"
self.worker_ident = None
else:
completed_ev.wait()
return False
def runcode(self):
"""Execute a code object.
Multithreaded wrapper around IPython's runcode()."""
global CODE_RUN
# we are in worker thread, stash out the id for runsource()
self.worker_ident = thread.get_ident()
if self._kill:
print >>Term.cout, 'Closing threads...',
Term.cout.flush()
for tokill in self.on_kill:
tokill()
print >>Term.cout, 'Done.'
# allow kill() to return
self._kill.set()
return True
# Install sigint handler. We do it every time to ensure that if user
# code modifies it, we restore our own handling.
try:
signal(SIGINT,sigint_handler)
except SystemError:
# This happens under Windows, which seems to have all sorts
# of problems with signal handling. Oh well...
pass
# Flush queue of pending code by calling the run methood of the parent
# class with all items which may be in the queue.
code_to_run = None
while 1:
try:
code_to_run, completed_ev, received_ev = self.code_queue.get_nowait()
except Queue.Empty:
break
received_ev.set()
# Exceptions need to be raised differently depending on which
# thread is active. This convoluted try/except is only there to
# protect against asynchronous exceptions, to ensure that a KBINT
# at the wrong time doesn't deadlock everything. The global
# CODE_TO_RUN is set to true/false as close as possible to the
# runcode() call, so that the KBINT handler is correctly informed.
try:
try:
CODE_RUN = True
InteractiveShell.runcode(self,code_to_run)
except KeyboardInterrupt:
print "Keyboard interrupted in mainloop"
while not self.code_queue.empty():
code, ev1,ev2 = self.code_queue.get_nowait()
ev1.set()
ev2.set()
break
finally:
CODE_RUN = False
# allow runsource() return from wait
completed_ev.set()
# This MUST return true for gtk threading to work
return True
def kill(self):
"""Kill the thread, returning when it has been shut down."""
self._kill = threading.Event()
self._kill.wait()
class MatplotlibShellBase:
"""Mixin class to provide the necessary modifications to regular IPython
shell classes for matplotlib support.
Given Python's MRO, this should be used as the FIRST class in the
inheritance hierarchy, so that it overrides the relevant methods."""
def _matplotlib_config(self,name,user_ns,user_global_ns=None):
"""Return items needed to setup the user's shell with matplotlib"""
# Initialize matplotlib to interactive mode always
import matplotlib
from matplotlib import backends
matplotlib.interactive(True)
def use(arg):
"""IPython wrapper for matplotlib's backend switcher.
In interactive use, we can not allow switching to a different
interactive backend, since thread conflicts will most likely crash
the python interpreter. This routine does a safety check first,
and refuses to perform a dangerous switch. It still allows
switching to non-interactive backends."""
if arg in backends.interactive_bk and arg != self.mpl_backend:
m=('invalid matplotlib backend switch.\n'
'This script attempted to switch to the interactive '
'backend: `%s`\n'
'Your current choice of interactive backend is: `%s`\n\n'
'Switching interactive matplotlib backends at runtime\n'
'would crash the python interpreter, '
'and IPython has blocked it.\n\n'
'You need to either change your choice of matplotlib backend\n'
'by editing your .matplotlibrc file, or run this script as a \n'
'standalone file from the command line, not using IPython.\n' %
(arg,self.mpl_backend) )
raise RuntimeError, m
else:
self.mpl_use(arg)
self.mpl_use._called = True
self.matplotlib = matplotlib
self.mpl_backend = matplotlib.rcParams['backend']
# we also need to block switching of interactive backends by use()
self.mpl_use = matplotlib.use
self.mpl_use._called = False
# overwrite the original matplotlib.use with our wrapper
matplotlib.use = use
# This must be imported last in the matplotlib series, after
# backend/interactivity choices have been made
import matplotlib.pylab as pylab
self.pylab = pylab
self.pylab.show._needmain = False
# We need to detect at runtime whether show() is called by the user.
# For this, we wrap it into a decorator which adds a 'called' flag.
self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
# Build a user namespace initialized with matplotlib/matlab features.
user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
user_global_ns)
# Import numpy as np/pyplot as plt are conventions we're trying to
# somewhat standardize on. Making them available to users by default
# will greatly help this.
exec ("import numpy\n"
"import numpy as np\n"
"import matplotlib\n"
"import matplotlib.pylab as pylab\n"
"try:\n"
" import matplotlib.pyplot as plt\n"
"except ImportError:\n"
" pass\n"
) in user_ns
# Build matplotlib info banner
b="""
Welcome to pylab, a matplotlib-based Python environment.
For more information, type 'help(pylab)'.
"""
return user_ns,user_global_ns,b
def mplot_exec(self,fname,*where,**kw):
"""Execute a matplotlib script.
This is a call to execfile(), but wrapped in safeties to properly
handle interactive rendering and backend switching."""
#print '*** Matplotlib runner ***' # dbg
# turn off rendering until end of script
isInteractive = self.matplotlib.rcParams['interactive']
self.matplotlib.interactive(False)
self.safe_execfile(fname,*where,**kw)
self.matplotlib.interactive(isInteractive)
# make rendering call now, if the user tried to do it
if self.pylab.draw_if_interactive.called:
self.pylab.draw()
self.pylab.draw_if_interactive.called = False
# if a backend switch was performed, reverse it now
if self.mpl_use._called:
self.matplotlib.rcParams['backend'] = self.mpl_backend
@testdec.skip_doctest
def magic_run(self,parameter_s=''):
Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
# Fix the docstring so users see the original as well
magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
"\n *** Modified %run for Matplotlib,"
" with proper interactive handling ***")
# Now we provide 2 versions of a matplotlib-aware IPython base shells, single
# and multithreaded. Note that these are meant for internal use, the IPShell*
# classes below are the ones meant for public consumption.
class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
"""Single-threaded shell with matplotlib support."""
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
user_ns=None,user_global_ns=None,**kw):
user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
banner2=b2,**kw)
class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
"""Multi-threaded shell with matplotlib support."""
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
user_ns=None,user_global_ns=None, **kw):
user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
banner2=b2,**kw)
#-----------------------------------------------------------------------------
# Utility functions for the different GUI enabled IPShell* classes.
def get_tk():
"""Tries to import Tkinter and returns a withdrawn Tkinter root
window. If Tkinter is already imported or not available, this
returns None. This function calls `hijack_tk` underneath.
"""
if not USE_TK or sys.modules.has_key('Tkinter'):
return None
else:
try:
import Tkinter
except ImportError:
return None
else:
hijack_tk()
r = Tkinter.Tk()
r.withdraw()
return r
def hijack_tk():
"""Modifies Tkinter's mainloop with a dummy so when a module calls
mainloop, it does not block.
"""
def misc_mainloop(self, n=0):
pass
def tkinter_mainloop(n=0):
pass
import Tkinter
Tkinter.Misc.mainloop = misc_mainloop
Tkinter.mainloop = tkinter_mainloop
def update_tk(tk):
"""Updates the Tkinter event loop. This is typically called from
the respective WX or GTK mainloops.
"""
if tk:
tk.update()
def hijack_wx():
"""Modifies wxPython's MainLoop with a dummy so user code does not
block IPython. The hijacked mainloop function is returned.
"""
def dummy_mainloop(*args, **kw):
pass
try:
import wx
except ImportError:
# For very old versions of WX
import wxPython as wx
ver = wx.__version__
orig_mainloop = None
if ver[:3] >= '2.5':
import wx
if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
elif hasattr(wx, '_core'): core = getattr(wx, '_core')
else: raise AttributeError('Could not find wx core module')
orig_mainloop = core.PyApp_MainLoop
core.PyApp_MainLoop = dummy_mainloop
elif ver[:3] == '2.4':
orig_mainloop = wx.wxc.wxPyApp_MainLoop
wx.wxc.wxPyApp_MainLoop = dummy_mainloop
else:
warn("Unable to find either wxPython version 2.4 or >= 2.5.")
return orig_mainloop
def hijack_gtk():
"""Modifies pyGTK's mainloop with a dummy so user code does not
block IPython. This function returns the original `gtk.mainloop`
function that has been hijacked.
"""
def dummy_mainloop(*args, **kw):
pass
import gtk
if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
else: orig_mainloop = gtk.mainloop
gtk.mainloop = dummy_mainloop
gtk.main = dummy_mainloop
return orig_mainloop
def hijack_qt():
"""Modifies PyQt's mainloop with a dummy so user code does not
block IPython. This function returns the original
`qt.qApp.exec_loop` function that has been hijacked.
"""
def dummy_mainloop(*args, **kw):
pass
import qt
orig_mainloop = qt.qApp.exec_loop
qt.qApp.exec_loop = dummy_mainloop
qt.QApplication.exec_loop = dummy_mainloop
return orig_mainloop
def hijack_qt4():
"""Modifies PyQt4's mainloop with a dummy so user code does not
block IPython. This function returns the original
`QtGui.qApp.exec_` function that has been hijacked.
"""
def dummy_mainloop(*args, **kw):
pass
from PyQt4 import QtGui, QtCore
orig_mainloop = QtGui.qApp.exec_
QtGui.qApp.exec_ = dummy_mainloop
QtGui.QApplication.exec_ = dummy_mainloop
QtCore.QCoreApplication.exec_ = dummy_mainloop
return orig_mainloop
#-----------------------------------------------------------------------------
# The IPShell* classes below are the ones meant to be run by external code as
# IPython instances. Note that unless a specific threading strategy is
# desired, the factory function start() below should be used instead (it
# selects the proper threaded class).
class IPThread(threading.Thread):
def run(self):
self.IP.mainloop(self._banner)
self.IP.kill()
class IPShellGTK(IPThread):
"""Run a gtk mainloop() in a separate thread.
Python commands can be passed to the thread where they will be executed.
This is implemented by periodically checking for passed code using a
GTK timeout callback."""
TIMEOUT = 100 # Millisecond interval between timeouts.
def __init__(self,argv=None,user_ns=None,user_global_ns=None,
debug=1,shell_class=MTInteractiveShell):
import gtk
# Check for set_interactive, coming up in new pygtk.
# Disable it so that this code works, but notify
# the user that he has a better option as well.
# XXX TODO better support when set_interactive is released
try:
gtk.set_interactive(False)
print "Your PyGtk has set_interactive(), so you can use the"
print "more stable single-threaded Gtk mode."
print "See https://bugs.launchpad.net/ipython/+bug/270856"
except AttributeError:
pass
self.gtk = gtk
self.gtk_mainloop = hijack_gtk()
# Allows us to use both Tk and GTK.
self.tk = get_tk()
if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
else: mainquit = self.gtk.mainquit
self.IP = make_IPython(argv,user_ns=user_ns,
user_global_ns=user_global_ns,
debug=debug,
shell_class=shell_class,
on_kill=[mainquit])
# HACK: slot for banner in self; it will be passed to the mainloop
# method only and .run() needs it. The actual value will be set by
# .mainloop().
self._banner = None
threading.Thread.__init__(self)
def mainloop(self,sys_exit=0,banner=None):
self._banner = banner
if self.gtk.pygtk_version >= (2,4,0):
import gobject
gobject.idle_add(self.on_timer)
else:
self.gtk.idle_add(self.on_timer)
if sys.platform != 'win32':
try:
if self.gtk.gtk_version[0] >= 2:
self.gtk.gdk.threads_init()
except AttributeError:
pass
except RuntimeError:
error('Your pyGTK likely has not been compiled with '
'threading support.\n'
'The exception printout is below.\n'
'You can either rebuild pyGTK with threads, or '
'try using \n'
'matplotlib with a different backend (like Tk or WX).\n'
'Note that matplotlib will most likely not work in its '
'current state!')
self.IP.InteractiveTB()
self.start()
self.gtk.gdk.threads_enter()
self.gtk_mainloop()
self.gtk.gdk.threads_leave()
self.join()
def on_timer(self):
"""Called when GTK is idle.
Must return True always, otherwise GTK stops calling it"""
update_tk(self.tk)
self.IP.runcode()
time.sleep(0.01)
return True
class IPShellWX(IPThread):
"""Run a wx mainloop() in a separate thread.
Python commands can be passed to the thread where they will be executed.
This is implemented by periodically checking for passed code using a
GTK timeout callback."""
TIMEOUT = 100 # Millisecond interval between timeouts.
def __init__(self,argv=None,user_ns=None,user_global_ns=None,
debug=1,shell_class=MTInteractiveShell):
self.IP = make_IPython(argv,user_ns=user_ns,
user_global_ns=user_global_ns,
debug=debug,
shell_class=shell_class,
on_kill=[self.wxexit])
wantedwxversion=self.IP.rc.wxversion
if wantedwxversion!="0":
try:
import wxversion
except ImportError:
error('The wxversion module is needed for WX version selection')
else:
try:
wxversion.select(wantedwxversion)
except:
self.IP.InteractiveTB()
error('Requested wxPython version %s could not be loaded' %
wantedwxversion)
import wx
threading.Thread.__init__(self)
self.wx = wx
self.wx_mainloop = hijack_wx()
# Allows us to use both Tk and GTK.
self.tk = get_tk()
# HACK: slot for banner in self; it will be passed to the mainloop
# method only and .run() needs it. The actual value will be set by
# .mainloop().
self._banner = None
self.app = None
def wxexit(self, *args):
if self.app is not None:
self.app.agent.timer.Stop()
self.app.ExitMainLoop()
def mainloop(self,sys_exit=0,banner=None):
self._banner = banner
self.start()
class TimerAgent(self.wx.MiniFrame):
wx = self.wx
IP = self.IP
tk = self.tk
def __init__(self, parent, interval):
style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ
self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
size=(100, 100),style=style)
self.Show(False)
self.interval = interval
self.timerId = self.wx.NewId()
def StartWork(self):
self.timer = self.wx.Timer(self, self.timerId)
self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
self.timer.Start(self.interval)
def OnTimer(self, event):
update_tk(self.tk)
self.IP.runcode()
class App(self.wx.App):
wx = self.wx
TIMEOUT = self.TIMEOUT
def OnInit(self):
'Create the main window and insert the custom frame'
self.agent = TimerAgent(None, self.TIMEOUT)
self.agent.Show(False)
self.agent.StartWork()
return True
self.app = App(redirect=False)
self.wx_mainloop(self.app)
self.join()
class IPShellQt(IPThread):
"""Run a Qt event loop in a separate thread.
Python commands can be passed to the thread where they will be executed.
This is implemented by periodically checking for passed code using a
Qt timer / slot."""
TIMEOUT = 100 # Millisecond interval between timeouts.
def __init__(self, argv=None, user_ns=None, user_global_ns=None,
debug=0, shell_class=MTInteractiveShell):
import qt
self.exec_loop = hijack_qt()
# Allows us to use both Tk and QT.
self.tk = get_tk()
self.IP = make_IPython(argv,
user_ns=user_ns,
user_global_ns=user_global_ns,
debug=debug,
shell_class=shell_class,
on_kill=[qt.qApp.exit])
# HACK: slot for banner in self; it will be passed to the mainloop
# method only and .run() needs it. The actual value will be set by
# .mainloop().
self._banner = None
threading.Thread.__init__(self)
def mainloop(self, sys_exit=0, banner=None):
import qt
self._banner = banner
if qt.QApplication.startingUp():
a = qt.QApplication(sys.argv)
self.timer = qt.QTimer()
qt.QObject.connect(self.timer,
qt.SIGNAL('timeout()'),
self.on_timer)
self.start()
self.timer.start(self.TIMEOUT, True)
while True:
if self.IP._kill: break
self.exec_loop()
self.join()
def on_timer(self):
update_tk(self.tk)
result = self.IP.runcode()
self.timer.start(self.TIMEOUT, True)
return result
class IPShellQt4(IPThread):
"""Run a Qt event loop in a separate thread.
Python commands can be passed to the thread where they will be executed.
This is implemented by periodically checking for passed code using a
Qt timer / slot."""
TIMEOUT = 100 # Millisecond interval between timeouts.
def __init__(self, argv=None, user_ns=None, user_global_ns=None,
debug=0, shell_class=MTInteractiveShell):
from PyQt4 import QtCore, QtGui
try:
# present in PyQt4-4.2.1 or later
QtCore.pyqtRemoveInputHook()
except AttributeError:
pass
if QtCore.PYQT_VERSION_STR == '4.3':
warn('''PyQt4 version 4.3 detected.
If you experience repeated threading warnings, please update PyQt4.
''')
self.exec_ = hijack_qt4()
# Allows us to use both Tk and QT.
self.tk = get_tk()
self.IP = make_IPython(argv,
user_ns=user_ns,
user_global_ns=user_global_ns,
debug=debug,
shell_class=shell_class,
on_kill=[QtGui.qApp.exit])
# HACK: slot for banner in self; it will be passed to the mainloop
# method only and .run() needs it. The actual value will be set by
# .mainloop().
self._banner = None
threading.Thread.__init__(self)
def mainloop(self, sys_exit=0, banner=None):
from PyQt4 import QtCore, QtGui
self._banner = banner
if QtGui.QApplication.startingUp():
a = QtGui.QApplication(sys.argv)
self.timer = QtCore.QTimer()
QtCore.QObject.connect(self.timer,
QtCore.SIGNAL('timeout()'),
self.on_timer)
self.start()
self.timer.start(self.TIMEOUT)
while True:
if self.IP._kill: break
self.exec_()
self.join()
def on_timer(self):
update_tk(self.tk)
result = self.IP.runcode()
self.timer.start(self.TIMEOUT)
return result
# A set of matplotlib public IPython shell classes, for single-threaded (Tk*
# and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use.
def _load_pylab(user_ns):
"""Allow users to disable pulling all of pylab into the top-level
namespace.
This little utility must be called AFTER the actual ipython instance is
running, since only then will the options file have been fully parsed."""
ip = IPython.ipapi.get()
if ip.options.pylab_import_all:
ip.ex("from matplotlib.pylab import *")
ip.IP.user_config_ns.update(ip.user_ns)
class IPShellMatplotlib(IPShell):
"""Subclass IPShell with MatplotlibShell as the internal shell.
Single-threaded class, meant for the Tk* and FLTK* backends.
Having this on a separate class simplifies the external driver code."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
shell_class=MatplotlibShell)
_load_pylab(self.IP.user_ns)
class IPShellMatplotlibGTK(IPShellGTK):
"""Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
Multi-threaded class, meant for the GTK* backends."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
shell_class=MatplotlibMTShell)
_load_pylab(self.IP.user_ns)
class IPShellMatplotlibWX(IPShellWX):
"""Subclass IPShellWX with MatplotlibMTShell as the internal shell.
Multi-threaded class, meant for the WX* backends."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
shell_class=MatplotlibMTShell)
_load_pylab(self.IP.user_ns)
class IPShellMatplotlibQt(IPShellQt):
"""Subclass IPShellQt with MatplotlibMTShell as the internal shell.
Multi-threaded class, meant for the Qt* backends."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
shell_class=MatplotlibMTShell)
_load_pylab(self.IP.user_ns)
class IPShellMatplotlibQt4(IPShellQt4):
"""Subclass IPShellQt4 with MatplotlibMTShell as the internal shell.
Multi-threaded class, meant for the Qt4* backends."""
def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug,
shell_class=MatplotlibMTShell)
_load_pylab(self.IP.user_ns)
#-----------------------------------------------------------------------------
# Factory functions to actually start the proper thread-aware shell
def _select_shell(argv):
"""Select a shell from the given argv vector.
This function implements the threading selection policy, allowing runtime
control of the threading mode, both for general users and for matplotlib.
Return:
Shell class to be instantiated for runtime operation.
"""
global USE_TK
mpl_shell = {'gthread' : IPShellMatplotlibGTK,
'wthread' : IPShellMatplotlibWX,
'qthread' : IPShellMatplotlibQt,
'q4thread' : IPShellMatplotlibQt4,
'tkthread' : IPShellMatplotlib, # Tk is built-in
}
th_shell = {'gthread' : IPShellGTK,
'wthread' : IPShellWX,
'qthread' : IPShellQt,
'q4thread' : IPShellQt4,
'tkthread' : IPShell, # Tk is built-in
}
backends = {'gthread' : 'GTKAgg',
'wthread' : 'WXAgg',
'qthread' : 'QtAgg',
'q4thread' :'Qt4Agg',
'tkthread' :'TkAgg',
}
all_opts = set(['tk','pylab','gthread','qthread','q4thread','wthread',
'tkthread'])
user_opts = set([s.replace('-','') for s in argv[:3]])
special_opts = user_opts & all_opts
if 'tk' in special_opts:
USE_TK = True
special_opts.remove('tk')
if 'pylab' in special_opts:
try:
import matplotlib
except ImportError:
error('matplotlib could NOT be imported! Starting normal IPython.')
return IPShell
special_opts.remove('pylab')
# If there's any option left, it means the user wants to force the
# threading backend, else it's auto-selected from the rc file
if special_opts:
th_mode = special_opts.pop()
matplotlib.rcParams['backend'] = backends[th_mode]
else:
backend = matplotlib.rcParams['backend']
if backend.startswith('GTK'):
th_mode = 'gthread'
elif backend.startswith('WX'):
th_mode = 'wthread'
elif backend.startswith('Qt4'):
th_mode = 'q4thread'
elif backend.startswith('Qt'):
th_mode = 'qthread'
else:
# Any other backend, use plain Tk
th_mode = 'tkthread'
return mpl_shell[th_mode]
else:
# No pylab requested, just plain threads
try:
th_mode = special_opts.pop()
except KeyError:
th_mode = 'tkthread'
return th_shell[th_mode]
# This is the one which should be called by external code.
def start(user_ns = None):
"""Return a running shell instance, dealing with threading options.
This is a factory function which will instantiate the proper IPython shell
based on the user's threading choice. Such a selector is needed because
different GUI toolkits require different thread handling details."""
shell = _select_shell(sys.argv)
return shell(user_ns = user_ns)
# Some aliases for backwards compatibility
IPythonShell = IPShell
IPythonShellEmbed = IPShellEmbed
#************************ End of file <Shell.py> ***************************
|