Show More
@@ -21,6 +21,7 b' from IPython.terminal.ipapp import load_default_config' | |||||
21 | from traitlets import Bool, CBool, Unicode |
|
21 | from traitlets import Bool, CBool, Unicode | |
22 | from IPython.utils.io import ask_yes_no |
|
22 | from IPython.utils.io import ask_yes_no | |
23 |
|
23 | |||
|
24 | class KillEmbeded(Exception):pass | |||
24 |
|
25 | |||
25 | # This is an additional magic that is exposed in embedded shells. |
|
26 | # This is an additional magic that is exposed in embedded shells. | |
26 | @magics_class |
|
27 | @magics_class | |
@@ -45,6 +46,39 b' class EmbeddedMagics(Magics):' | |||||
45 | print ("This embedded IPython will not reactivate anymore " |
|
46 | print ("This embedded IPython will not reactivate anymore " | |
46 | "once you exit.") |
|
47 | "once you exit.") | |
47 |
|
48 | |||
|
49 | @line_magic | |||
|
50 | def raise_on_exit(self, parameter_s=''): | |||
|
51 | """%raise_on_exit [True|False]: Make the current embeded kernel to raise an exception on exit. | |||
|
52 | ||||
|
53 | You can change that again during current session by calling `%raise_on_exit` with `True`/`False` as parameter | |||
|
54 | ||||
|
55 | This function (after asking for confirmation) sets an internal flag so | |||
|
56 | that an embedded IPython will raise a `KillEmbeded` Exception on exit. This is useful to | |||
|
57 | permanently exit a loop that create IPython embed instance. | |||
|
58 | """ | |||
|
59 | parameter_s = parameter_s.strip().lower() | |||
|
60 | ||||
|
61 | should_raise = None | |||
|
62 | if parameter_s in {'yes', 'raise', 'true', '1'}: | |||
|
63 | should_raise = True | |||
|
64 | elif parameter_s in {'no', 'false', '0', 'None'}: | |||
|
65 | should_raise = False | |||
|
66 | else: | |||
|
67 | if self.shell.should_raise : | |||
|
68 | print("The current embed instance will raise on exit, use `%raise_on_exit False` to disable") | |||
|
69 | return | |||
|
70 | else: | |||
|
71 | print("The current embed instance will not raise on exit, use `%raise_on_exit True` to enable") | |||
|
72 | return | |||
|
73 | ||||
|
74 | if should_raise: | |||
|
75 | self.shell.should_raise = True | |||
|
76 | print ("This embedded IPython will raise while exiting.") | |||
|
77 | else : | |||
|
78 | self.shell.should_raise = False | |||
|
79 | print ("This embedded IPython will not raise while exiting.") | |||
|
80 | ||||
|
81 | ||||
48 |
|
82 | |||
49 | class InteractiveShellEmbed(TerminalInteractiveShell): |
|
83 | class InteractiveShellEmbed(TerminalInteractiveShell): | |
50 |
|
84 | |||
@@ -52,6 +86,7 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
52 | exit_msg = Unicode('') |
|
86 | exit_msg = Unicode('') | |
53 | embedded = CBool(True) |
|
87 | embedded = CBool(True) | |
54 | embedded_active = CBool(True) |
|
88 | embedded_active = CBool(True) | |
|
89 | should_raise = CBool(False) | |||
55 | # Like the base class display_banner is not configurable, but here it |
|
90 | # Like the base class display_banner is not configurable, but here it | |
56 | # is True by default. |
|
91 | # is True by default. | |
57 | display_banner = CBool(True) |
|
92 | display_banner = CBool(True) | |
@@ -130,6 +165,10 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
130 | if self.exit_msg is not None: |
|
165 | if self.exit_msg is not None: | |
131 | print(self.exit_msg) |
|
166 | print(self.exit_msg) | |
132 |
|
167 | |||
|
168 | if self.should_raise: | |||
|
169 | raise KillEmbeded('This instance has been marked as must raise on exit.') | |||
|
170 | ||||
|
171 | ||||
133 | def mainloop(self, local_ns=None, module=None, stack_depth=0, |
|
172 | def mainloop(self, local_ns=None, module=None, stack_depth=0, | |
134 | display_banner=None, global_ns=None, compile_flags=None): |
|
173 | display_banner=None, global_ns=None, compile_flags=None): | |
135 | """Embeds IPython into a running python program. |
|
174 | """Embeds IPython into a running python program. |
General Comments 0
You need to be logged in to leave comments.
Login now