##// END OF EJS Templates
- Fix a bug introduced by r1357 which broke embedding....
fperez -
Show More
@@ -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 2222 2007-04-06 17:11:27Z fperez $"""
7 $Id: Shell.py 2577 2007-08-02 23:50:02Z 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>
@@ -39,8 +39,8 b' except ImportError:'
39
39
40 # IPython imports
40 # IPython imports
41 import IPython
41 import IPython
42 from IPython import ultraTB
42 from IPython import ultraTB, ipapi
43 from IPython.genutils import Term,warn,error,flag_calls
43 from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
44 from IPython.iplib import InteractiveShell
44 from IPython.iplib import InteractiveShell
45 from IPython.ipmaker import make_IPython
45 from IPython.ipmaker import make_IPython
46 from IPython.Magic import Magic
46 from IPython.Magic import Magic
@@ -79,6 +79,22 b' class IPShell:'
79 sys.exit()
79 sys.exit()
80
80
81 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
82 def kill_embedded(self,parameter_s=''):
83 """%kill_embedded : deactivate for good the current embedded IPython.
84
85 This function (after asking for confirmation) sets an internal flag so that
86 an embedded IPython will never activate again. This is useful to
87 permanently disable a shell that is being called inside a loop: once you've
88 figured out what you needed from it, you may then kill it and the program
89 will then continue to run without the interactive shell interfering again.
90 """
91
92 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
93 "(y/n)? [y/N] ",'n')
94 if kill:
95 self.shell.embedded_active = False
96 print "This embedded IPython will not reactivate anymore once you exit."
97
82 class IPShellEmbed:
98 class IPShellEmbed:
83 """Allow embedding an IPython shell into a running program.
99 """Allow embedding an IPython shell into a running program.
84
100
@@ -153,6 +169,9 b' class IPShellEmbed:'
153 embedded=True,
169 embedded=True,
154 user_ns=user_ns)
170 user_ns=user_ns)
155
171
172 ip = ipapi.IPApi(self.IP)
173 ip.expose_magic("kill_embedded",kill_embedded)
174
156 # copy our own displayhook also
175 # copy our own displayhook also
157 self.sys_displayhook_embed = sys.displayhook
176 self.sys_displayhook_embed = sys.displayhook
158 # and leave the system's display hook clean
177 # and leave the system's display hook clean
@@ -196,6 +215,14 b' class IPShellEmbed:'
196 The optional keyword parameter dummy controls whether the call
215 The optional keyword parameter dummy controls whether the call
197 actually does anything. """
216 actually does anything. """
198
217
218 # If the user has turned it off, go away
219 if not self.IP.embedded_active:
220 return
221
222 # Normal exits from interactive mode set this flag, so the shell can't
223 # re-enter (it checks this variable at the start of interactive mode).
224 self.IP.exit_now = False
225
199 # Allow the dummy parameter to override the global __dummy_mode
226 # Allow the dummy parameter to override the global __dummy_mode
200 if dummy or (dummy != 0 and self.__dummy_mode):
227 if dummy or (dummy != 0 and self.__dummy_mode):
201 return
228 return
@@ -5,7 +5,7 b' General purpose utilities.'
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 2568 2007-07-29 21:38:44Z fperez $"""
8 $Id: genutils.py 2577 2007-08-02 23:50:02Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -1055,7 +1055,7 b" def raw_input_ext(prompt='', ps2='... '):"
1055
1055
1056 #----------------------------------------------------------------------------
1056 #----------------------------------------------------------------------------
1057 def ask_yes_no(prompt,default=None):
1057 def ask_yes_no(prompt,default=None):
1058 """Asks a question and returns an integer 1/0 (y/n) answer.
1058 """Asks a question and returns a boolean (y/n) answer.
1059
1059
1060 If default is given (one of 'y','n'), it is used if the user input is
1060 If default is given (one of 'y','n'), it is used if the user input is
1061 empty. Otherwise the question is repeated until an answer is given.
1061 empty. Otherwise the question is repeated until an answer is given.
@@ -6,7 +6,7 b' Requires Python 2.3 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 2571 2007-08-01 14:48:03Z vivainio $
9 $Id: iplib.py 2577 2007-08-02 23:50:02Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -228,6 +228,10 b' class InteractiveShell(object,Magic):'
228 # We need to know whether the instance is meant for embedding, since
228 # We need to know whether the instance is meant for embedding, since
229 # global/local namespaces need to be handled differently in that case
229 # global/local namespaces need to be handled differently in that case
230 self.embedded = embedded
230 self.embedded = embedded
231 if embedded:
232 # Control variable so users can, from within the embedded instance,
233 # permanently deactivate it.
234 self.embedded_active = True
231
235
232 # command compiler
236 # command compiler
233 self.compile = codeop.CommandCompiler()
237 self.compile = codeop.CommandCompiler()
@@ -1566,7 +1570,7 b' want to merge them back into the new files.""" % locals()'
1566 # all names in the builtin namespace needed by ipython point to
1570 # all names in the builtin namespace needed by ipython point to
1567 # ourselves, and not to other instances.
1571 # ourselves, and not to other instances.
1568 self.add_builtins()
1572 self.add_builtins()
1569
1573
1570 self.interact(header)
1574 self.interact(header)
1571
1575
1572 # now, purge out the user namespace from anything we might have added
1576 # now, purge out the user namespace from anything we might have added
@@ -1,3 +1,17 b''
1 2007-08-02 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Shell.py (IPShellEmbed.__call__): fix bug introduced in
4 r1357, which had killed multiple invocations of an embedded
5 ipython (this means that example-embed has been broken for over 1
6 year!!!). Rather than possibly breaking the batch stuff for which
7 the code in iplib.py/interact was introduced, I worked around the
8 problem in the embedding class in Shell.py. We really need a
9 bloody test suite for this code, I'm sick of finding stuff that
10 used to work breaking left and right every time I use an old
11 feature I hadn't touched in a few months.
12 (kill_embedded): Add a new magic that only shows up in embedded
13 mode, to allow users to permanently deactivate an embedded instance.
14
1 2007-08-01 Ville Vainio <vivainio@gmail.com>
15 2007-08-01 Ville Vainio <vivainio@gmail.com>
2
16
3 * iplib.py, ipy_profile_sh.py (runlines): Fix the bug where raw
17 * iplib.py, ipy_profile_sh.py (runlines): Fix the bug where raw
@@ -54,7 +54,9 b" print '\\nHello. This is printed from the main controller program.\\n'"
54 # You can then call ipshell() anywhere you need it (with an optional
54 # You can then call ipshell() anywhere you need it (with an optional
55 # message):
55 # message):
56 ipshell('***Called from top level. '
56 ipshell('***Called from top level. '
57 'Hit Ctrl-D to exit interpreter and continue program.')
57 'Hit Ctrl-D to exit interpreter and continue program.\n'
58 'Note that if you use %kill_embedded, you can fully deactivate\n'
59 'This embedded instance so it will never turn on again')
58
60
59 print '\nBack in caller program, moving along...\n'
61 print '\nBack in caller program, moving along...\n'
60
62
General Comments 0
You need to be logged in to leave comments. Login now