##// END OF EJS Templates
Merge branch 'exit-autocall'
Thomas Kluyver -
r3726:a48dd85f merge
parent child Browse files
Show More
@@ -7,6 +7,7 b' Authors:'
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 * Thomas Kluyver
10 11
11 12 Notes
12 13 -----
@@ -34,6 +35,10 b' class IPyAutocall(object):'
34 35 This happens regardless of 'autocall' variable state. Use this to
35 36 develop macro-like mechanisms.
36 37 """
38 _ip = None
39 rewrite = True
40 def __init__(self, ip=None):
41 self._ip = ip
37 42
38 43 def set_ip(self,ip):
39 44 """ Will be used to set _ip point to current ipython instance b/f call
@@ -43,3 +48,24 b' class IPyAutocall(object):'
43 48 """
44 49 self._ip = ip
45 50
51
52 class ExitAutocall(IPyAutocall):
53 """An autocallable object which will be added to the user namespace so that
54 exit, exit(), quit or quit() are all valid ways to close the shell."""
55 rewrite = False
56
57 def __call__(self):
58 self._ip.ask_exit()
59
60 class ZMQExitAutocall(ExitAutocall):
61 """Exit IPython. Autocallable, so it needn't be explicitly called.
62
63 Parameters
64 ----------
65 keep_kernel : bool
66 If True, leave the kernel alive. Otherwise, tell the kernel to exit too
67 (default).
68 """
69 def __call__(self, keep_kernel=False):
70 self._ip.keepkernel_on_exit = keep_kernel
71 self._ip.ask_exit()
@@ -39,6 +39,7 b' from IPython.core import prefilter'
39 39 from IPython.core import shadowns
40 40 from IPython.core import ultratb
41 41 from IPython.core.alias import AliasManager
42 from IPython.core.autocall import ExitAutocall
42 43 from IPython.core.builtin_trap import BuiltinTrap
43 44 from IPython.core.compilerop import CachingCompiler
44 45 from IPython.core.display_trap import DisplayTrap
@@ -198,6 +199,9 b' class InteractiveShell(Configurable, Magic):'
198 199 display_pub_class = Type(DisplayPublisher)
199 200
200 201 exit_now = CBool(False)
202 exiter = Instance(ExitAutocall)
203 def _exiter_default(self):
204 return ExitAutocall(self)
201 205 # Monotonically increasing execution counter
202 206 execution_count = Int(1)
203 207 filename = Unicode("<ipython console>")
@@ -1024,6 +1028,9 b' class InteractiveShell(Configurable, Magic):'
1024 1028 # Store myself as the public api!!!
1025 1029 ns['get_ipython'] = self.get_ipython
1026 1030
1031 ns['exit'] = self.exiter
1032 ns['quit'] = self.exiter
1033
1027 1034 # Sync what we've added so far to user_ns_hidden so these aren't seen
1028 1035 # by %who
1029 1036 self.user_ns_hidden.update(ns)
@@ -2504,14 +2504,6 b' Defaulting color scheme to \'NoColor\'"""'
2504 2504 print 'Pretty printing has been turned', \
2505 2505 ['OFF','ON'][ptformatter.pprint]
2506 2506
2507 def magic_Exit(self, parameter_s=''):
2508 """Exit IPython."""
2509
2510 self.shell.ask_exit()
2511
2512 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2513 magic_exit = magic_quit = magic_Quit = magic_Exit
2514
2515 2507 #......................................................................
2516 2508 # Functions to implement unix shell-type things
2517 2509
@@ -894,7 +894,7 b' class AutoHandler(PrefilterHandler):'
894 894 return line
895 895
896 896 force_auto = isinstance(obj, IPyAutocall)
897 auto_rewrite = True
897 auto_rewrite = getattr(obj, 'rewrite', True)
898 898
899 899 if pre == ESC_QUOTE:
900 900 # Auto-quote splitting on whitespace
@@ -77,7 +77,7 b' for i in range(5):'
77 77
78 78 print "that's all folks!"
79 79
80 %Exit
80 exit
81 81 """
82 82 output = """\
83 83 In [1]: print 'hello, this is python'
@@ -124,7 +124,7 b' In [11]: print "that\'s all folks!"'
124 124 that's all folks!
125 125
126 126
127 In [12]: %Exit
127 In [12]: exit
128 128 """
129 129 runner = irunner.IPythonRunner(out=self.out)
130 130 self._test_runner(runner,source,output)
@@ -24,6 +24,7 b' from IPython.core.interactiveshell import ('
24 24 InteractiveShell, InteractiveShellABC
25 25 )
26 26 from IPython.core import page
27 from IPython.core.autocall import ZMQExitAutocall
27 28 from IPython.core.displayhook import DisplayHook
28 29 from IPython.core.displaypub import DisplayPublisher
29 30 from IPython.core.macro import Macro
@@ -105,6 +106,10 b' class ZMQInteractiveShell(InteractiveShell):'
105 106 displayhook_class = Type(ZMQDisplayHook)
106 107 display_pub_class = Type(ZMQDisplayPublisher)
107 108
109 exiter = Instance(ZMQExitAutocall)
110 def _exiter_default(self):
111 return ZMQExitAutocall(self)
112
108 113 keepkernel_on_exit = None
109 114
110 115 def init_environment(self):
@@ -593,15 +598,4 b' class ZMQInteractiveShell(InteractiveShell):'
593 598 )
594 599 self.payload_manager.write_payload(payload)
595 600
596 def magic_Exit(self, parameter_s=''):
597 """Exit IPython. If the -k option is provided, the kernel will be left
598 running. Otherwise, it will shutdown without prompting.
599 """
600 opts,args = self.parse_options(parameter_s,'k')
601 self.shell.keepkernel_on_exit = opts.has_key('k')
602 self.shell.ask_exit()
603
604 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
605 magic_exit = magic_quit = magic_Quit = magic_Exit
606
607 601 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now