##// 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 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10 * Thomas Kluyver
10
11
11 Notes
12 Notes
12 -----
13 -----
@@ -34,8 +35,12 b' class IPyAutocall(object):'
34 This happens regardless of 'autocall' variable state. Use this to
35 This happens regardless of 'autocall' variable state. Use this to
35 develop macro-like mechanisms.
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 def set_ip(self,ip):
43 def set_ip(self, ip):
39 """ Will be used to set _ip point to current ipython instance b/f call
44 """ Will be used to set _ip point to current ipython instance b/f call
40
45
41 Override this method if you don't want this to happen.
46 Override this method if you don't want this to happen.
@@ -43,3 +48,24 b' class IPyAutocall(object):'
43 """
48 """
44 self._ip = ip
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 from IPython.core import shadowns
39 from IPython.core import shadowns
40 from IPython.core import ultratb
40 from IPython.core import ultratb
41 from IPython.core.alias import AliasManager
41 from IPython.core.alias import AliasManager
42 from IPython.core.autocall import ExitAutocall
42 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.compilerop import CachingCompiler
44 from IPython.core.compilerop import CachingCompiler
44 from IPython.core.display_trap import DisplayTrap
45 from IPython.core.display_trap import DisplayTrap
@@ -198,6 +199,9 b' class InteractiveShell(Configurable, Magic):'
198 display_pub_class = Type(DisplayPublisher)
199 display_pub_class = Type(DisplayPublisher)
199
200
200 exit_now = CBool(False)
201 exit_now = CBool(False)
202 exiter = Instance(ExitAutocall)
203 def _exiter_default(self):
204 return ExitAutocall(self)
201 # Monotonically increasing execution counter
205 # Monotonically increasing execution counter
202 execution_count = Int(1)
206 execution_count = Int(1)
203 filename = Unicode("<ipython console>")
207 filename = Unicode("<ipython console>")
@@ -1023,6 +1027,9 b' class InteractiveShell(Configurable, Magic):'
1023
1027
1024 # Store myself as the public api!!!
1028 # Store myself as the public api!!!
1025 ns['get_ipython'] = self.get_ipython
1029 ns['get_ipython'] = self.get_ipython
1030
1031 ns['exit'] = self.exiter
1032 ns['quit'] = self.exiter
1026
1033
1027 # Sync what we've added so far to user_ns_hidden so these aren't seen
1034 # Sync what we've added so far to user_ns_hidden so these aren't seen
1028 # by %who
1035 # by %who
@@ -2503,14 +2503,6 b' Defaulting color scheme to \'NoColor\'"""'
2503 ptformatter.pprint = bool(1 - ptformatter.pprint)
2503 ptformatter.pprint = bool(1 - ptformatter.pprint)
2504 print 'Pretty printing has been turned', \
2504 print 'Pretty printing has been turned', \
2505 ['OFF','ON'][ptformatter.pprint]
2505 ['OFF','ON'][ptformatter.pprint]
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
2506
2515 #......................................................................
2507 #......................................................................
2516 # Functions to implement unix shell-type things
2508 # Functions to implement unix shell-type things
@@ -894,7 +894,7 b' class AutoHandler(PrefilterHandler):'
894 return line
894 return line
895
895
896 force_auto = isinstance(obj, IPyAutocall)
896 force_auto = isinstance(obj, IPyAutocall)
897 auto_rewrite = True
897 auto_rewrite = getattr(obj, 'rewrite', True)
898
898
899 if pre == ESC_QUOTE:
899 if pre == ESC_QUOTE:
900 # Auto-quote splitting on whitespace
900 # Auto-quote splitting on whitespace
@@ -77,7 +77,7 b' for i in range(5):'
77
77
78 print "that's all folks!"
78 print "that's all folks!"
79
79
80 %Exit
80 exit
81 """
81 """
82 output = """\
82 output = """\
83 In [1]: print 'hello, this is python'
83 In [1]: print 'hello, this is python'
@@ -124,7 +124,7 b' In [11]: print "that\'s all folks!"'
124 that's all folks!
124 that's all folks!
125
125
126
126
127 In [12]: %Exit
127 In [12]: exit
128 """
128 """
129 runner = irunner.IPythonRunner(out=self.out)
129 runner = irunner.IPythonRunner(out=self.out)
130 self._test_runner(runner,source,output)
130 self._test_runner(runner,source,output)
@@ -24,6 +24,7 b' from IPython.core.interactiveshell import ('
24 InteractiveShell, InteractiveShellABC
24 InteractiveShell, InteractiveShellABC
25 )
25 )
26 from IPython.core import page
26 from IPython.core import page
27 from IPython.core.autocall import ZMQExitAutocall
27 from IPython.core.displayhook import DisplayHook
28 from IPython.core.displayhook import DisplayHook
28 from IPython.core.displaypub import DisplayPublisher
29 from IPython.core.displaypub import DisplayPublisher
29 from IPython.core.macro import Macro
30 from IPython.core.macro import Macro
@@ -104,6 +105,10 b' class ZMQInteractiveShell(InteractiveShell):'
104
105
105 displayhook_class = Type(ZMQDisplayHook)
106 displayhook_class = Type(ZMQDisplayHook)
106 display_pub_class = Type(ZMQDisplayPublisher)
107 display_pub_class = Type(ZMQDisplayPublisher)
108
109 exiter = Instance(ZMQExitAutocall)
110 def _exiter_default(self):
111 return ZMQExitAutocall(self)
107
112
108 keepkernel_on_exit = None
113 keepkernel_on_exit = None
109
114
@@ -592,16 +597,5 b' class ZMQInteractiveShell(InteractiveShell):'
592 text=content
597 text=content
593 )
598 )
594 self.payload_manager.write_payload(payload)
599 self.payload_manager.write_payload(payload)
595
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
600
607 InteractiveShellABC.register(ZMQInteractiveShell)
601 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now