##// END OF EJS Templates
Implement %doctest_mode magic in zmqshell with payload....
Fernando Perez -
Show More
@@ -3254,15 +3254,20 b' Defaulting color scheme to \'NoColor\'"""'
3254 def magic_doctest_mode(self,parameter_s=''):
3254 def magic_doctest_mode(self,parameter_s=''):
3255 """Toggle doctest mode on and off.
3255 """Toggle doctest mode on and off.
3256
3256
3257 This mode allows you to toggle the prompt behavior between normal
3257 This mode is intended to make IPython behave as much as possible like a
3258 IPython prompts and ones that are as similar to the default IPython
3258 plain Python shell, from the perspective of how its prompts, exceptions
3259 interpreter as possible.
3259 and output look. This makes it easy to copy and paste parts of a
3260
3260 session into doctests. It does so by:
3261 It also supports the pasting of code snippets that have leading '>>>'
3261
3262 and '...' prompts in them. This means that you can paste doctests from
3262 - Changing the prompts to the classic ``>>>`` ones.
3263 files or docstrings (even if they have leading whitespace), and the
3263 - Changing the exception reporting mode to 'Plain'.
3264 code will execute correctly. You can then use '%history -tn' to see
3264 - Disabling pretty-printing of output.
3265 the translated history without line numbers; this will give you the
3265
3266 Note that IPython also supports the pasting of code snippets that have
3267 leading '>>>' and '...' prompts in them. This means that you can paste
3268 doctests from files or docstrings (even if they have leading
3269 whitespace), and the code will execute correctly. You can then use
3270 '%history -t' to see the translated history; this will give you the
3266 input after removal of all the leading prompts and whitespace, which
3271 input after removal of all the leading prompts and whitespace, which
3267 can be pasted back into an editor.
3272 can be pasted back into an editor.
3268
3273
@@ -3308,7 +3313,6 b' Defaulting color scheme to \'NoColor\'"""'
3308 shell.pprint = False
3313 shell.pprint = False
3309
3314
3310 shell.magic_xmode('Plain')
3315 shell.magic_xmode('Plain')
3311
3312 else:
3316 else:
3313 # turn off
3317 # turn off
3314 oc.prompt1.p_template = shell.prompt_in1
3318 oc.prompt1.p_template = shell.prompt_in1
@@ -3329,8 +3333,8 b' Defaulting color scheme to \'NoColor\'"""'
3329
3333
3330 # Store new mode and inform
3334 # Store new mode and inform
3331 dstore.mode = bool(1-int(mode))
3335 dstore.mode = bool(1-int(mode))
3332 print 'Doctest mode is:',
3336 mode_label = ['OFF','ON'][dstore.mode]
3333 print ['OFF','ON'][dstore.mode]
3337 print 'Doctest mode is:', mode_label
3334
3338
3335 def magic_gui(self, parameter_s=''):
3339 def magic_gui(self, parameter_s=''):
3336 """Enable or disable IPython GUI event loop integration.
3340 """Enable or disable IPython GUI event loop integration.
@@ -86,6 +86,65 b' class ZMQInteractiveShell(InteractiveShell):'
86 Term = IPython.utils.io.IOTerm()
86 Term = IPython.utils.io.IOTerm()
87 IPython.utils.io.Term = Term
87 IPython.utils.io.Term = Term
88
88
89 def magic_doctest_mode(self,parameter_s=''):
90 """Toggle doctest mode on and off.
91
92 This mode is intended to make IPython behave as much as possible like a
93 plain Python shell, from the perspective of how its prompts, exceptions
94 and output look. This makes it easy to copy and paste parts of a
95 session into doctests. It does so by:
96
97 - Changing the prompts to the classic ``>>>`` ones.
98 - Changing the exception reporting mode to 'Plain'.
99 - Disabling pretty-printing of output.
100
101 Note that IPython also supports the pasting of code snippets that have
102 leading '>>>' and '...' prompts in them. This means that you can paste
103 doctests from files or docstrings (even if they have leading
104 whitespace), and the code will execute correctly. You can then use
105 '%history -t' to see the translated history; this will give you the
106 input after removal of all the leading prompts and whitespace, which
107 can be pasted back into an editor.
108
109 With these features, you can switch into this mode easily whenever you
110 need to do testing and changes to doctests, without having to leave
111 your existing IPython session.
112 """
113
114 from IPython.utils.ipstruct import Struct
115
116 # Shorthands
117 shell = self.shell
118 # dstore is a data store kept in the instance metadata bag to track any
119 # changes we make, so we can undo them later.
120 dstore = shell.meta.setdefault('doctest_mode', Struct())
121 save_dstore = dstore.setdefault
122
123 # save a few values we'll need to recover later
124 mode = save_dstore('mode', False)
125 save_dstore('rc_pprint', shell.pprint)
126 save_dstore('xmode', shell.InteractiveTB.mode)
127
128 if mode == False:
129 # turn on
130 shell.pprint = False
131 shell.magic_xmode('Plain')
132 else:
133 # turn off
134 shell.pprint = dstore.rc_pprint
135 shell.magic_xmode(dstore.xmode)
136
137 # Store new mode and inform on console
138 dstore.mode = bool(1-int(mode))
139 mode_label = ['OFF','ON'][dstore.mode]
140 print('Doctest mode is:', mode_label)
141
142 # Send the payload back so that clients can modify their prompt display
143 payload = dict(
144 source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode',
145 mode=dstore.mode)
146 self.payload_manager.write_payload(payload)
147
89 def magic_edit(self,parameter_s='',last_call=['','']):
148 def magic_edit(self,parameter_s='',last_call=['','']):
90 """Bring up an editor and execute the resulting code.
149 """Bring up an editor and execute the resulting code.
91
150
General Comments 0
You need to be logged in to leave comments. Login now