Show More
@@ -1,612 +1,611 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Subclass of InteractiveShell for terminal based frontends.""" |
|
2 | """Subclass of InteractiveShell for terminal based frontends.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2010 The IPython Development Team |
|
7 | # Copyright (C) 2008-2010 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import __builtin__ |
|
17 | import __builtin__ | |
18 | import bdb |
|
18 | import bdb | |
19 | import os |
|
19 | import os | |
20 | import re |
|
20 | import re | |
21 | import sys |
|
21 | import sys | |
22 |
|
22 | |||
23 | try: |
|
23 | try: | |
24 | from contextlib import nested |
|
24 | from contextlib import nested | |
25 | except: |
|
25 | except: | |
26 | from IPython.utils.nested_context import nested |
|
26 | from IPython.utils.nested_context import nested | |
27 |
|
27 | |||
28 | from IPython.core.error import TryNext |
|
28 | from IPython.core.error import TryNext | |
29 | from IPython.core.usage import interactive_usage, default_banner |
|
29 | from IPython.core.usage import interactive_usage, default_banner | |
30 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC |
|
30 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC | |
31 | from IPython.lib.inputhook import enable_gui |
|
31 | from IPython.lib.inputhook import enable_gui | |
32 | from IPython.lib.pylabtools import pylab_activate |
|
32 | from IPython.lib.pylabtools import pylab_activate | |
33 | from IPython.testing.skipdoctest import skip_doctest |
|
33 | from IPython.testing.skipdoctest import skip_doctest | |
34 | from IPython.utils import py3compat |
|
34 | from IPython.utils import py3compat | |
35 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
35 | from IPython.utils.terminal import toggle_set_term_title, set_term_title | |
36 | from IPython.utils.process import abbrev_cwd |
|
36 | from IPython.utils.process import abbrev_cwd | |
37 | from IPython.utils.warn import warn |
|
37 | from IPython.utils.warn import warn | |
38 | from IPython.utils.text import num_ini_spaces |
|
38 | from IPython.utils.text import num_ini_spaces | |
39 | from IPython.utils.traitlets import Int, CBool, Unicode |
|
39 | from IPython.utils.traitlets import Int, CBool, Unicode | |
40 |
|
40 | |||
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 | # Utilities |
|
42 | # Utilities | |
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 | def get_default_editor(): |
|
45 | def get_default_editor(): | |
46 | try: |
|
46 | try: | |
47 | ed = os.environ['EDITOR'] |
|
47 | ed = os.environ['EDITOR'] | |
48 | except KeyError: |
|
48 | except KeyError: | |
49 | if os.name == 'posix': |
|
49 | if os.name == 'posix': | |
50 | ed = 'vi' # the only one guaranteed to be there! |
|
50 | ed = 'vi' # the only one guaranteed to be there! | |
51 | else: |
|
51 | else: | |
52 | ed = 'notepad' # same in Windows! |
|
52 | ed = 'notepad' # same in Windows! | |
53 | return ed |
|
53 | return ed | |
54 |
|
54 | |||
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 | # Main class |
|
56 | # Main class | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 | class TerminalInteractiveShell(InteractiveShell): |
|
59 | class TerminalInteractiveShell(InteractiveShell): | |
60 |
|
60 | |||
61 | autoedit_syntax = CBool(False, config=True, |
|
61 | autoedit_syntax = CBool(False, config=True, | |
62 | help="auto editing of files with syntax errors.") |
|
62 | help="auto editing of files with syntax errors.") | |
63 | banner = Unicode('') |
|
63 | banner = Unicode('') | |
64 | banner1 = Unicode(default_banner, config=True, |
|
64 | banner1 = Unicode(default_banner, config=True, | |
65 | help="""The part of the banner to be printed before the profile""" |
|
65 | help="""The part of the banner to be printed before the profile""" | |
66 | ) |
|
66 | ) | |
67 | banner2 = Unicode('', config=True, |
|
67 | banner2 = Unicode('', config=True, | |
68 | help="""The part of the banner to be printed after the profile""" |
|
68 | help="""The part of the banner to be printed after the profile""" | |
69 | ) |
|
69 | ) | |
70 | confirm_exit = CBool(True, config=True, |
|
70 | confirm_exit = CBool(True, config=True, | |
71 | help=""" |
|
71 | help=""" | |
72 | Set to confirm when you try to exit IPython with an EOF (Control-D |
|
72 | Set to confirm when you try to exit IPython with an EOF (Control-D | |
73 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
73 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
74 | you can force a direct exit without any confirmation.""", |
|
74 | you can force a direct exit without any confirmation.""", | |
75 | ) |
|
75 | ) | |
76 | # This display_banner only controls whether or not self.show_banner() |
|
76 | # This display_banner only controls whether or not self.show_banner() | |
77 | # is called when mainloop/interact are called. The default is False |
|
77 | # is called when mainloop/interact are called. The default is False | |
78 | # because for the terminal based application, the banner behavior |
|
78 | # because for the terminal based application, the banner behavior | |
79 | # is controlled by Global.display_banner, which IPythonApp looks at |
|
79 | # is controlled by Global.display_banner, which IPythonApp looks at | |
80 | # to determine if *it* should call show_banner() by hand or not. |
|
80 | # to determine if *it* should call show_banner() by hand or not. | |
81 | display_banner = CBool(False) # This isn't configurable! |
|
81 | display_banner = CBool(False) # This isn't configurable! | |
82 | embedded = CBool(False) |
|
82 | embedded = CBool(False) | |
83 | embedded_active = CBool(False) |
|
83 | embedded_active = CBool(False) | |
84 | editor = Unicode(get_default_editor(), config=True, |
|
84 | editor = Unicode(get_default_editor(), config=True, | |
85 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." |
|
85 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." | |
86 | ) |
|
86 | ) | |
87 | pager = Unicode('less', config=True, |
|
87 | pager = Unicode('less', config=True, | |
88 | help="The shell program to be used for paging.") |
|
88 | help="The shell program to be used for paging.") | |
89 |
|
89 | |||
90 | screen_length = Int(0, config=True, |
|
90 | screen_length = Int(0, config=True, | |
91 | help= |
|
91 | help= | |
92 | """Number of lines of your screen, used to control printing of very |
|
92 | """Number of lines of your screen, used to control printing of very | |
93 | long strings. Strings longer than this number of lines will be sent |
|
93 | long strings. Strings longer than this number of lines will be sent | |
94 | through a pager instead of directly printed. The default value for |
|
94 | through a pager instead of directly printed. The default value for | |
95 | this is 0, which means IPython will auto-detect your screen size every |
|
95 | this is 0, which means IPython will auto-detect your screen size every | |
96 | time it needs to print certain potentially long strings (this doesn't |
|
96 | time it needs to print certain potentially long strings (this doesn't | |
97 | change the behavior of the 'print' keyword, it's only triggered |
|
97 | change the behavior of the 'print' keyword, it's only triggered | |
98 | internally). If for some reason this isn't working well (it needs |
|
98 | internally). If for some reason this isn't working well (it needs | |
99 | curses support), specify it yourself. Otherwise don't change the |
|
99 | curses support), specify it yourself. Otherwise don't change the | |
100 | default.""", |
|
100 | default.""", | |
101 | ) |
|
101 | ) | |
102 | term_title = CBool(False, config=True, |
|
102 | term_title = CBool(False, config=True, | |
103 | help="Enable auto setting the terminal title." |
|
103 | help="Enable auto setting the terminal title." | |
104 | ) |
|
104 | ) | |
105 |
|
105 | |||
106 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, user_ns=None, |
|
106 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, user_ns=None, | |
107 | user_global_ns=None, custom_exceptions=((),None), |
|
107 | user_global_ns=None, custom_exceptions=((),None), | |
108 | usage=None, banner1=None, banner2=None, |
|
108 | usage=None, banner1=None, banner2=None, | |
109 | display_banner=None): |
|
109 | display_banner=None): | |
110 |
|
110 | |||
111 | super(TerminalInteractiveShell, self).__init__( |
|
111 | super(TerminalInteractiveShell, self).__init__( | |
112 | config=config, profile_dir=profile_dir, user_ns=user_ns, |
|
112 | config=config, profile_dir=profile_dir, user_ns=user_ns, | |
113 | user_global_ns=user_global_ns, custom_exceptions=custom_exceptions |
|
113 | user_global_ns=user_global_ns, custom_exceptions=custom_exceptions | |
114 | ) |
|
114 | ) | |
115 | # use os.system instead of utils.process.system by default, except on Windows |
|
115 | # use os.system instead of utils.process.system by default, except on Windows | |
116 | if os.name == 'nt': |
|
116 | if os.name == 'nt': | |
117 | self.system = self.system_piped |
|
117 | self.system = self.system_piped | |
118 | else: |
|
118 | else: | |
119 | self.system = self.system_raw |
|
119 | self.system = self.system_raw | |
120 |
|
120 | |||
121 | self.init_term_title() |
|
121 | self.init_term_title() | |
122 | self.init_usage(usage) |
|
122 | self.init_usage(usage) | |
123 | self.init_banner(banner1, banner2, display_banner) |
|
123 | self.init_banner(banner1, banner2, display_banner) | |
124 |
|
124 | |||
125 | #------------------------------------------------------------------------- |
|
125 | #------------------------------------------------------------------------- | |
126 | # Things related to the terminal |
|
126 | # Things related to the terminal | |
127 | #------------------------------------------------------------------------- |
|
127 | #------------------------------------------------------------------------- | |
128 |
|
128 | |||
129 | @property |
|
129 | @property | |
130 | def usable_screen_length(self): |
|
130 | def usable_screen_length(self): | |
131 | if self.screen_length == 0: |
|
131 | if self.screen_length == 0: | |
132 | return 0 |
|
132 | return 0 | |
133 | else: |
|
133 | else: | |
134 | num_lines_bot = self.separate_in.count('\n')+1 |
|
134 | num_lines_bot = self.separate_in.count('\n')+1 | |
135 | return self.screen_length - num_lines_bot |
|
135 | return self.screen_length - num_lines_bot | |
136 |
|
136 | |||
137 | def init_term_title(self): |
|
137 | def init_term_title(self): | |
138 | # Enable or disable the terminal title. |
|
138 | # Enable or disable the terminal title. | |
139 | if self.term_title: |
|
139 | if self.term_title: | |
140 | toggle_set_term_title(True) |
|
140 | toggle_set_term_title(True) | |
141 | set_term_title('IPython: ' + abbrev_cwd()) |
|
141 | set_term_title('IPython: ' + abbrev_cwd()) | |
142 | else: |
|
142 | else: | |
143 | toggle_set_term_title(False) |
|
143 | toggle_set_term_title(False) | |
144 |
|
144 | |||
145 | #------------------------------------------------------------------------- |
|
145 | #------------------------------------------------------------------------- | |
146 | # Things related to aliases |
|
146 | # Things related to aliases | |
147 | #------------------------------------------------------------------------- |
|
147 | #------------------------------------------------------------------------- | |
148 |
|
148 | |||
149 | def init_alias(self): |
|
149 | def init_alias(self): | |
150 | # The parent class defines aliases that can be safely used with any |
|
150 | # The parent class defines aliases that can be safely used with any | |
151 | # frontend. |
|
151 | # frontend. | |
152 | super(TerminalInteractiveShell, self).init_alias() |
|
152 | super(TerminalInteractiveShell, self).init_alias() | |
153 |
|
153 | |||
154 | # Now define aliases that only make sense on the terminal, because they |
|
154 | # Now define aliases that only make sense on the terminal, because they | |
155 | # need direct access to the console in a way that we can't emulate in |
|
155 | # need direct access to the console in a way that we can't emulate in | |
156 | # GUI or web frontend |
|
156 | # GUI or web frontend | |
157 | if os.name == 'posix': |
|
157 | if os.name == 'posix': | |
158 | aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'), |
|
158 | aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'), | |
159 | ('man', 'man')] |
|
159 | ('man', 'man')] | |
160 | elif os.name == 'nt': |
|
160 | elif os.name == 'nt': | |
161 | aliases = [('cls', 'cls')] |
|
161 | aliases = [('cls', 'cls')] | |
162 |
|
162 | |||
163 |
|
163 | |||
164 | for name, cmd in aliases: |
|
164 | for name, cmd in aliases: | |
165 | self.alias_manager.define_alias(name, cmd) |
|
165 | self.alias_manager.define_alias(name, cmd) | |
166 |
|
166 | |||
167 | #------------------------------------------------------------------------- |
|
167 | #------------------------------------------------------------------------- | |
168 | # Things related to the banner and usage |
|
168 | # Things related to the banner and usage | |
169 | #------------------------------------------------------------------------- |
|
169 | #------------------------------------------------------------------------- | |
170 |
|
170 | |||
171 | def _banner1_changed(self): |
|
171 | def _banner1_changed(self): | |
172 | self.compute_banner() |
|
172 | self.compute_banner() | |
173 |
|
173 | |||
174 | def _banner2_changed(self): |
|
174 | def _banner2_changed(self): | |
175 | self.compute_banner() |
|
175 | self.compute_banner() | |
176 |
|
176 | |||
177 | def _term_title_changed(self, name, new_value): |
|
177 | def _term_title_changed(self, name, new_value): | |
178 | self.init_term_title() |
|
178 | self.init_term_title() | |
179 |
|
179 | |||
180 | def init_banner(self, banner1, banner2, display_banner): |
|
180 | def init_banner(self, banner1, banner2, display_banner): | |
181 | if banner1 is not None: |
|
181 | if banner1 is not None: | |
182 | self.banner1 = banner1 |
|
182 | self.banner1 = banner1 | |
183 | if banner2 is not None: |
|
183 | if banner2 is not None: | |
184 | self.banner2 = banner2 |
|
184 | self.banner2 = banner2 | |
185 | if display_banner is not None: |
|
185 | if display_banner is not None: | |
186 | self.display_banner = display_banner |
|
186 | self.display_banner = display_banner | |
187 | self.compute_banner() |
|
187 | self.compute_banner() | |
188 |
|
188 | |||
189 | def show_banner(self, banner=None): |
|
189 | def show_banner(self, banner=None): | |
190 | if banner is None: |
|
190 | if banner is None: | |
191 | banner = self.banner |
|
191 | banner = self.banner | |
192 | self.write(banner) |
|
192 | self.write(banner) | |
193 |
|
193 | |||
194 | def compute_banner(self): |
|
194 | def compute_banner(self): | |
195 | self.banner = self.banner1 |
|
195 | self.banner = self.banner1 | |
196 | if self.profile and self.profile != 'default': |
|
196 | if self.profile and self.profile != 'default': | |
197 | self.banner += '\nIPython profile: %s\n' % self.profile |
|
197 | self.banner += '\nIPython profile: %s\n' % self.profile | |
198 | if self.banner2: |
|
198 | if self.banner2: | |
199 | self.banner += '\n' + self.banner2 |
|
199 | self.banner += '\n' + self.banner2 | |
200 |
|
200 | |||
201 | def init_usage(self, usage=None): |
|
201 | def init_usage(self, usage=None): | |
202 | if usage is None: |
|
202 | if usage is None: | |
203 | self.usage = interactive_usage |
|
203 | self.usage = interactive_usage | |
204 | else: |
|
204 | else: | |
205 | self.usage = usage |
|
205 | self.usage = usage | |
206 |
|
206 | |||
207 | #------------------------------------------------------------------------- |
|
207 | #------------------------------------------------------------------------- | |
208 | # Mainloop and code execution logic |
|
208 | # Mainloop and code execution logic | |
209 | #------------------------------------------------------------------------- |
|
209 | #------------------------------------------------------------------------- | |
210 |
|
210 | |||
211 | def mainloop(self, display_banner=None): |
|
211 | def mainloop(self, display_banner=None): | |
212 | """Start the mainloop. |
|
212 | """Start the mainloop. | |
213 |
|
213 | |||
214 | If an optional banner argument is given, it will override the |
|
214 | If an optional banner argument is given, it will override the | |
215 | internally created default banner. |
|
215 | internally created default banner. | |
216 | """ |
|
216 | """ | |
217 |
|
217 | |||
218 | with nested(self.builtin_trap, self.display_trap): |
|
218 | with nested(self.builtin_trap, self.display_trap): | |
219 |
|
219 | |||
220 | while 1: |
|
220 | while 1: | |
221 | try: |
|
221 | try: | |
222 | self.interact(display_banner=display_banner) |
|
222 | self.interact(display_banner=display_banner) | |
223 | #self.interact_with_readline() |
|
223 | #self.interact_with_readline() | |
224 | # XXX for testing of a readline-decoupled repl loop, call |
|
224 | # XXX for testing of a readline-decoupled repl loop, call | |
225 | # interact_with_readline above |
|
225 | # interact_with_readline above | |
226 | break |
|
226 | break | |
227 | except KeyboardInterrupt: |
|
227 | except KeyboardInterrupt: | |
228 | # this should not be necessary, but KeyboardInterrupt |
|
228 | # this should not be necessary, but KeyboardInterrupt | |
229 | # handling seems rather unpredictable... |
|
229 | # handling seems rather unpredictable... | |
230 | self.write("\nKeyboardInterrupt in interact()\n") |
|
230 | self.write("\nKeyboardInterrupt in interact()\n") | |
231 |
|
231 | |||
232 |
def _replace_rlhist_multiline(self, source_raw |
|
232 | def _replace_rlhist_multiline(self, source_raw): | |
233 | """Store multiple lines as a single entry in history""" |
|
233 | """Store multiple lines as a single entry in history""" | |
234 | if self.multiline_history and self.has_readline: |
|
234 | if self.multiline_history and self.has_readline: | |
235 | hlen = self.readline.get_current_history_length() |
|
235 | hlen = self.readline.get_current_history_length() | |
236 | for i in range(hlen - hlen_before_cell): |
|
236 | for i in range(hlen - self.hlen_before_cell): | |
237 | self.readline.remove_history_item(hlen - i - 1) |
|
237 | self.readline.remove_history_item(hlen - i - 1) | |
238 | stdin_encoding = sys.stdin.encoding or "utf-8" |
|
238 | stdin_encoding = sys.stdin.encoding or "utf-8" | |
239 | self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(), |
|
239 | self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(), | |
240 | stdin_encoding)) |
|
240 | stdin_encoding)) | |
|
241 | self.hlen_before_cell = self.readline.get_current_history_length() | |||
241 |
|
242 | |||
242 | def interact(self, display_banner=None): |
|
243 | def interact(self, display_banner=None): | |
243 | """Closely emulate the interactive Python console.""" |
|
244 | """Closely emulate the interactive Python console.""" | |
244 |
|
245 | |||
245 | # batch run -> do not interact |
|
246 | # batch run -> do not interact | |
246 | if self.exit_now: |
|
247 | if self.exit_now: | |
247 | return |
|
248 | return | |
248 |
|
249 | |||
249 | if display_banner is None: |
|
250 | if display_banner is None: | |
250 | display_banner = self.display_banner |
|
251 | display_banner = self.display_banner | |
251 |
|
252 | |||
252 | if isinstance(display_banner, basestring): |
|
253 | if isinstance(display_banner, basestring): | |
253 | self.show_banner(display_banner) |
|
254 | self.show_banner(display_banner) | |
254 | elif display_banner: |
|
255 | elif display_banner: | |
255 | self.show_banner() |
|
256 | self.show_banner() | |
256 |
|
257 | |||
257 | more = False |
|
258 | more = False | |
258 | hlen_before_cell = self.readline.get_current_history_length() |
|
|||
259 |
|
259 | |||
260 | # Mark activity in the builtins |
|
260 | # Mark activity in the builtins | |
261 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
261 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
262 |
|
262 | |||
263 | if self.has_readline: |
|
263 | if self.has_readline: | |
264 | self.readline_startup_hook(self.pre_readline) |
|
264 | self.readline_startup_hook(self.pre_readline) | |
|
265 | self.hlen_before_cell = self.readline.get_current_history_length() | |||
265 | # exit_now is set by a call to %Exit or %Quit, through the |
|
266 | # exit_now is set by a call to %Exit or %Quit, through the | |
266 | # ask_exit callback. |
|
267 | # ask_exit callback. | |
267 |
|
268 | |||
268 | while not self.exit_now: |
|
269 | while not self.exit_now: | |
269 | self.hooks.pre_prompt_hook() |
|
270 | self.hooks.pre_prompt_hook() | |
270 | if more: |
|
271 | if more: | |
271 | try: |
|
272 | try: | |
272 | prompt = self.hooks.generate_prompt(True) |
|
273 | prompt = self.hooks.generate_prompt(True) | |
273 | except: |
|
274 | except: | |
274 | self.showtraceback() |
|
275 | self.showtraceback() | |
275 | if self.autoindent: |
|
276 | if self.autoindent: | |
276 | self.rl_do_indent = True |
|
277 | self.rl_do_indent = True | |
277 |
|
278 | |||
278 | else: |
|
279 | else: | |
279 | try: |
|
280 | try: | |
280 | prompt = self.hooks.generate_prompt(False) |
|
281 | prompt = self.hooks.generate_prompt(False) | |
281 | except: |
|
282 | except: | |
282 | self.showtraceback() |
|
283 | self.showtraceback() | |
283 | try: |
|
284 | try: | |
284 | line = self.raw_input(prompt) |
|
285 | line = self.raw_input(prompt) | |
285 | if self.exit_now: |
|
286 | if self.exit_now: | |
286 | # quick exit on sys.std[in|out] close |
|
287 | # quick exit on sys.std[in|out] close | |
287 | break |
|
288 | break | |
288 | if self.autoindent: |
|
289 | if self.autoindent: | |
289 | self.rl_do_indent = False |
|
290 | self.rl_do_indent = False | |
290 |
|
291 | |||
291 | except KeyboardInterrupt: |
|
292 | except KeyboardInterrupt: | |
292 | #double-guard against keyboardinterrupts during kbdint handling |
|
293 | #double-guard against keyboardinterrupts during kbdint handling | |
293 | try: |
|
294 | try: | |
294 | self.write('\nKeyboardInterrupt\n') |
|
295 | self.write('\nKeyboardInterrupt\n') | |
295 | source_raw = self.input_splitter.source_raw_reset()[1] |
|
296 | source_raw = self.input_splitter.source_raw_reset()[1] | |
296 |
self._replace_rlhist_multiline(source_raw |
|
297 | self._replace_rlhist_multiline(source_raw) | |
297 | hlen_before_cell = self.readline.get_current_history_length() |
|
|||
298 | more = False |
|
298 | more = False | |
299 | except KeyboardInterrupt: |
|
299 | except KeyboardInterrupt: | |
300 | pass |
|
300 | pass | |
301 | except EOFError: |
|
301 | except EOFError: | |
302 | if self.autoindent: |
|
302 | if self.autoindent: | |
303 | self.rl_do_indent = False |
|
303 | self.rl_do_indent = False | |
304 | if self.has_readline: |
|
304 | if self.has_readline: | |
305 | self.readline_startup_hook(None) |
|
305 | self.readline_startup_hook(None) | |
306 | self.write('\n') |
|
306 | self.write('\n') | |
307 | self.exit() |
|
307 | self.exit() | |
308 | except bdb.BdbQuit: |
|
308 | except bdb.BdbQuit: | |
309 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
309 | warn('The Python debugger has exited with a BdbQuit exception.\n' | |
310 | 'Because of how pdb handles the stack, it is impossible\n' |
|
310 | 'Because of how pdb handles the stack, it is impossible\n' | |
311 | 'for IPython to properly format this particular exception.\n' |
|
311 | 'for IPython to properly format this particular exception.\n' | |
312 | 'IPython will resume normal operation.') |
|
312 | 'IPython will resume normal operation.') | |
313 | except: |
|
313 | except: | |
314 | # exceptions here are VERY RARE, but they can be triggered |
|
314 | # exceptions here are VERY RARE, but they can be triggered | |
315 | # asynchronously by signal handlers, for example. |
|
315 | # asynchronously by signal handlers, for example. | |
316 | self.showtraceback() |
|
316 | self.showtraceback() | |
317 | else: |
|
317 | else: | |
318 | self.input_splitter.push(line) |
|
318 | self.input_splitter.push(line) | |
319 | more = self.input_splitter.push_accepts_more() |
|
319 | more = self.input_splitter.push_accepts_more() | |
320 | if (self.SyntaxTB.last_syntax_error and |
|
320 | if (self.SyntaxTB.last_syntax_error and | |
321 | self.autoedit_syntax): |
|
321 | self.autoedit_syntax): | |
322 | self.edit_syntax_error() |
|
322 | self.edit_syntax_error() | |
323 | if not more: |
|
323 | if not more: | |
324 | source_raw = self.input_splitter.source_raw_reset()[1] |
|
324 | source_raw = self.input_splitter.source_raw_reset()[1] | |
325 |
self._replace_rlhist_multiline(source_raw |
|
325 | self._replace_rlhist_multiline(source_raw) | |
326 | hlen_before_cell = self.readline.get_current_history_length() |
|
|||
327 | self.run_cell(source_raw, store_history=True) |
|
326 | self.run_cell(source_raw, store_history=True) | |
328 |
|
327 | |||
329 | # We are off again... |
|
328 | # We are off again... | |
330 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
329 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
331 |
|
330 | |||
332 | # Turn off the exit flag, so the mainloop can be restarted if desired |
|
331 | # Turn off the exit flag, so the mainloop can be restarted if desired | |
333 | self.exit_now = False |
|
332 | self.exit_now = False | |
334 |
|
333 | |||
335 | def raw_input(self, prompt=''): |
|
334 | def raw_input(self, prompt=''): | |
336 | """Write a prompt and read a line. |
|
335 | """Write a prompt and read a line. | |
337 |
|
336 | |||
338 | The returned line does not include the trailing newline. |
|
337 | The returned line does not include the trailing newline. | |
339 | When the user enters the EOF key sequence, EOFError is raised. |
|
338 | When the user enters the EOF key sequence, EOFError is raised. | |
340 |
|
339 | |||
341 | Optional inputs: |
|
340 | Optional inputs: | |
342 |
|
341 | |||
343 | - prompt(''): a string to be printed to prompt the user. |
|
342 | - prompt(''): a string to be printed to prompt the user. | |
344 |
|
343 | |||
345 | - continue_prompt(False): whether this line is the first one or a |
|
344 | - continue_prompt(False): whether this line is the first one or a | |
346 | continuation in a sequence of inputs. |
|
345 | continuation in a sequence of inputs. | |
347 | """ |
|
346 | """ | |
348 | # Code run by the user may have modified the readline completer state. |
|
347 | # Code run by the user may have modified the readline completer state. | |
349 | # We must ensure that our completer is back in place. |
|
348 | # We must ensure that our completer is back in place. | |
350 |
|
349 | |||
351 | if self.has_readline: |
|
350 | if self.has_readline: | |
352 | self.set_readline_completer() |
|
351 | self.set_readline_completer() | |
353 |
|
352 | |||
354 | try: |
|
353 | try: | |
355 | line = py3compat.str_to_unicode(self.raw_input_original(prompt)) |
|
354 | line = py3compat.str_to_unicode(self.raw_input_original(prompt)) | |
356 | except ValueError: |
|
355 | except ValueError: | |
357 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
356 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
358 | " or sys.stdout.close()!\nExiting IPython!") |
|
357 | " or sys.stdout.close()!\nExiting IPython!") | |
359 | self.ask_exit() |
|
358 | self.ask_exit() | |
360 | return "" |
|
359 | return "" | |
361 |
|
360 | |||
362 | # Try to be reasonably smart about not re-indenting pasted input more |
|
361 | # Try to be reasonably smart about not re-indenting pasted input more | |
363 | # than necessary. We do this by trimming out the auto-indent initial |
|
362 | # than necessary. We do this by trimming out the auto-indent initial | |
364 | # spaces, if the user's actual input started itself with whitespace. |
|
363 | # spaces, if the user's actual input started itself with whitespace. | |
365 | if self.autoindent: |
|
364 | if self.autoindent: | |
366 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
365 | if num_ini_spaces(line) > self.indent_current_nsp: | |
367 | line = line[self.indent_current_nsp:] |
|
366 | line = line[self.indent_current_nsp:] | |
368 | self.indent_current_nsp = 0 |
|
367 | self.indent_current_nsp = 0 | |
369 |
|
368 | |||
370 | return line |
|
369 | return line | |
371 |
|
370 | |||
372 | #------------------------------------------------------------------------- |
|
371 | #------------------------------------------------------------------------- | |
373 | # Methods to support auto-editing of SyntaxErrors. |
|
372 | # Methods to support auto-editing of SyntaxErrors. | |
374 | #------------------------------------------------------------------------- |
|
373 | #------------------------------------------------------------------------- | |
375 |
|
374 | |||
376 | def edit_syntax_error(self): |
|
375 | def edit_syntax_error(self): | |
377 | """The bottom half of the syntax error handler called in the main loop. |
|
376 | """The bottom half of the syntax error handler called in the main loop. | |
378 |
|
377 | |||
379 | Loop until syntax error is fixed or user cancels. |
|
378 | Loop until syntax error is fixed or user cancels. | |
380 | """ |
|
379 | """ | |
381 |
|
380 | |||
382 | while self.SyntaxTB.last_syntax_error: |
|
381 | while self.SyntaxTB.last_syntax_error: | |
383 | # copy and clear last_syntax_error |
|
382 | # copy and clear last_syntax_error | |
384 | err = self.SyntaxTB.clear_err_state() |
|
383 | err = self.SyntaxTB.clear_err_state() | |
385 | if not self._should_recompile(err): |
|
384 | if not self._should_recompile(err): | |
386 | return |
|
385 | return | |
387 | try: |
|
386 | try: | |
388 | # may set last_syntax_error again if a SyntaxError is raised |
|
387 | # may set last_syntax_error again if a SyntaxError is raised | |
389 | self.safe_execfile(err.filename,self.user_ns) |
|
388 | self.safe_execfile(err.filename,self.user_ns) | |
390 | except: |
|
389 | except: | |
391 | self.showtraceback() |
|
390 | self.showtraceback() | |
392 | else: |
|
391 | else: | |
393 | try: |
|
392 | try: | |
394 | f = file(err.filename) |
|
393 | f = file(err.filename) | |
395 | try: |
|
394 | try: | |
396 | # This should be inside a display_trap block and I |
|
395 | # This should be inside a display_trap block and I | |
397 | # think it is. |
|
396 | # think it is. | |
398 | sys.displayhook(f.read()) |
|
397 | sys.displayhook(f.read()) | |
399 | finally: |
|
398 | finally: | |
400 | f.close() |
|
399 | f.close() | |
401 | except: |
|
400 | except: | |
402 | self.showtraceback() |
|
401 | self.showtraceback() | |
403 |
|
402 | |||
404 | def _should_recompile(self,e): |
|
403 | def _should_recompile(self,e): | |
405 | """Utility routine for edit_syntax_error""" |
|
404 | """Utility routine for edit_syntax_error""" | |
406 |
|
405 | |||
407 | if e.filename in ('<ipython console>','<input>','<string>', |
|
406 | if e.filename in ('<ipython console>','<input>','<string>', | |
408 | '<console>','<BackgroundJob compilation>', |
|
407 | '<console>','<BackgroundJob compilation>', | |
409 | None): |
|
408 | None): | |
410 |
|
409 | |||
411 | return False |
|
410 | return False | |
412 | try: |
|
411 | try: | |
413 | if (self.autoedit_syntax and |
|
412 | if (self.autoedit_syntax and | |
414 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
413 | not self.ask_yes_no('Return to editor to correct syntax error? ' | |
415 | '[Y/n] ','y')): |
|
414 | '[Y/n] ','y')): | |
416 | return False |
|
415 | return False | |
417 | except EOFError: |
|
416 | except EOFError: | |
418 | return False |
|
417 | return False | |
419 |
|
418 | |||
420 | def int0(x): |
|
419 | def int0(x): | |
421 | try: |
|
420 | try: | |
422 | return int(x) |
|
421 | return int(x) | |
423 | except TypeError: |
|
422 | except TypeError: | |
424 | return 0 |
|
423 | return 0 | |
425 | # always pass integer line and offset values to editor hook |
|
424 | # always pass integer line and offset values to editor hook | |
426 | try: |
|
425 | try: | |
427 | self.hooks.fix_error_editor(e.filename, |
|
426 | self.hooks.fix_error_editor(e.filename, | |
428 | int0(e.lineno),int0(e.offset),e.msg) |
|
427 | int0(e.lineno),int0(e.offset),e.msg) | |
429 | except TryNext: |
|
428 | except TryNext: | |
430 | warn('Could not open editor') |
|
429 | warn('Could not open editor') | |
431 | return False |
|
430 | return False | |
432 | return True |
|
431 | return True | |
433 |
|
432 | |||
434 | #------------------------------------------------------------------------- |
|
433 | #------------------------------------------------------------------------- | |
435 | # Things related to GUI support and pylab |
|
434 | # Things related to GUI support and pylab | |
436 | #------------------------------------------------------------------------- |
|
435 | #------------------------------------------------------------------------- | |
437 |
|
436 | |||
438 | def enable_pylab(self, gui=None, import_all=True): |
|
437 | def enable_pylab(self, gui=None, import_all=True): | |
439 | """Activate pylab support at runtime. |
|
438 | """Activate pylab support at runtime. | |
440 |
|
439 | |||
441 | This turns on support for matplotlib, preloads into the interactive |
|
440 | This turns on support for matplotlib, preloads into the interactive | |
442 | namespace all of numpy and pylab, and configures IPython to correcdtly |
|
441 | namespace all of numpy and pylab, and configures IPython to correcdtly | |
443 | interact with the GUI event loop. The GUI backend to be used can be |
|
442 | interact with the GUI event loop. The GUI backend to be used can be | |
444 | optionally selected with the optional :param:`gui` argument. |
|
443 | optionally selected with the optional :param:`gui` argument. | |
445 |
|
444 | |||
446 | Parameters |
|
445 | Parameters | |
447 | ---------- |
|
446 | ---------- | |
448 | gui : optional, string |
|
447 | gui : optional, string | |
449 |
|
448 | |||
450 | If given, dictates the choice of matplotlib GUI backend to use |
|
449 | If given, dictates the choice of matplotlib GUI backend to use | |
451 | (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or |
|
450 | (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or | |
452 | 'gtk'), otherwise we use the default chosen by matplotlib (as |
|
451 | 'gtk'), otherwise we use the default chosen by matplotlib (as | |
453 | dictated by the matplotlib build-time options plus the user's |
|
452 | dictated by the matplotlib build-time options plus the user's | |
454 | matplotlibrc configuration file). |
|
453 | matplotlibrc configuration file). | |
455 | """ |
|
454 | """ | |
456 | # We want to prevent the loading of pylab to pollute the user's |
|
455 | # We want to prevent the loading of pylab to pollute the user's | |
457 | # namespace as shown by the %who* magics, so we execute the activation |
|
456 | # namespace as shown by the %who* magics, so we execute the activation | |
458 | # code in an empty namespace, and we update *both* user_ns and |
|
457 | # code in an empty namespace, and we update *both* user_ns and | |
459 | # user_ns_hidden with this information. |
|
458 | # user_ns_hidden with this information. | |
460 | ns = {} |
|
459 | ns = {} | |
461 | gui = pylab_activate(ns, gui, import_all) |
|
460 | gui = pylab_activate(ns, gui, import_all) | |
462 | self.user_ns.update(ns) |
|
461 | self.user_ns.update(ns) | |
463 | self.user_ns_hidden.update(ns) |
|
462 | self.user_ns_hidden.update(ns) | |
464 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
463 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
465 | # plot updates into account |
|
464 | # plot updates into account | |
466 | enable_gui(gui) |
|
465 | enable_gui(gui) | |
467 | self.magic_run = self._pylab_magic_run |
|
466 | self.magic_run = self._pylab_magic_run | |
468 |
|
467 | |||
469 | #------------------------------------------------------------------------- |
|
468 | #------------------------------------------------------------------------- | |
470 | # Things related to exiting |
|
469 | # Things related to exiting | |
471 | #------------------------------------------------------------------------- |
|
470 | #------------------------------------------------------------------------- | |
472 |
|
471 | |||
473 | def ask_exit(self): |
|
472 | def ask_exit(self): | |
474 | """ Ask the shell to exit. Can be overiden and used as a callback. """ |
|
473 | """ Ask the shell to exit. Can be overiden and used as a callback. """ | |
475 | self.exit_now = True |
|
474 | self.exit_now = True | |
476 |
|
475 | |||
477 | def exit(self): |
|
476 | def exit(self): | |
478 | """Handle interactive exit. |
|
477 | """Handle interactive exit. | |
479 |
|
478 | |||
480 | This method calls the ask_exit callback.""" |
|
479 | This method calls the ask_exit callback.""" | |
481 | if self.confirm_exit: |
|
480 | if self.confirm_exit: | |
482 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
481 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
483 | self.ask_exit() |
|
482 | self.ask_exit() | |
484 | else: |
|
483 | else: | |
485 | self.ask_exit() |
|
484 | self.ask_exit() | |
486 |
|
485 | |||
487 | #------------------------------------------------------------------------ |
|
486 | #------------------------------------------------------------------------ | |
488 | # Magic overrides |
|
487 | # Magic overrides | |
489 | #------------------------------------------------------------------------ |
|
488 | #------------------------------------------------------------------------ | |
490 | # Once the base class stops inheriting from magic, this code needs to be |
|
489 | # Once the base class stops inheriting from magic, this code needs to be | |
491 | # moved into a separate machinery as well. For now, at least isolate here |
|
490 | # moved into a separate machinery as well. For now, at least isolate here | |
492 | # the magics which this class needs to implement differently from the base |
|
491 | # the magics which this class needs to implement differently from the base | |
493 | # class, or that are unique to it. |
|
492 | # class, or that are unique to it. | |
494 |
|
493 | |||
495 | def magic_autoindent(self, parameter_s = ''): |
|
494 | def magic_autoindent(self, parameter_s = ''): | |
496 | """Toggle autoindent on/off (if available).""" |
|
495 | """Toggle autoindent on/off (if available).""" | |
497 |
|
496 | |||
498 | self.shell.set_autoindent() |
|
497 | self.shell.set_autoindent() | |
499 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
498 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] | |
500 |
|
499 | |||
501 | @skip_doctest |
|
500 | @skip_doctest | |
502 | def magic_cpaste(self, parameter_s=''): |
|
501 | def magic_cpaste(self, parameter_s=''): | |
503 | """Paste & execute a pre-formatted code block from clipboard. |
|
502 | """Paste & execute a pre-formatted code block from clipboard. | |
504 |
|
503 | |||
505 | You must terminate the block with '--' (two minus-signs) or Ctrl-D alone on the |
|
504 | You must terminate the block with '--' (two minus-signs) or Ctrl-D alone on the | |
506 | line. You can also provide your own sentinel with '%paste -s %%' ('%%' |
|
505 | line. You can also provide your own sentinel with '%paste -s %%' ('%%' | |
507 | is the new sentinel for this operation) |
|
506 | is the new sentinel for this operation) | |
508 |
|
507 | |||
509 | The block is dedented prior to execution to enable execution of method |
|
508 | The block is dedented prior to execution to enable execution of method | |
510 | definitions. '>' and '+' characters at the beginning of a line are |
|
509 | definitions. '>' and '+' characters at the beginning of a line are | |
511 | ignored, to allow pasting directly from e-mails, diff files and |
|
510 | ignored, to allow pasting directly from e-mails, diff files and | |
512 | doctests (the '...' continuation prompt is also stripped). The |
|
511 | doctests (the '...' continuation prompt is also stripped). The | |
513 | executed block is also assigned to variable named 'pasted_block' for |
|
512 | executed block is also assigned to variable named 'pasted_block' for | |
514 | later editing with '%edit pasted_block'. |
|
513 | later editing with '%edit pasted_block'. | |
515 |
|
514 | |||
516 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. |
|
515 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. | |
517 | This assigns the pasted block to variable 'foo' as string, without |
|
516 | This assigns the pasted block to variable 'foo' as string, without | |
518 | dedenting or executing it (preceding >>> and + is still stripped) |
|
517 | dedenting or executing it (preceding >>> and + is still stripped) | |
519 |
|
518 | |||
520 | '%cpaste -r' re-executes the block previously entered by cpaste. |
|
519 | '%cpaste -r' re-executes the block previously entered by cpaste. | |
521 |
|
520 | |||
522 | Do not be alarmed by garbled output on Windows (it's a readline bug). |
|
521 | Do not be alarmed by garbled output on Windows (it's a readline bug). | |
523 | Just press enter and type -- (and press enter again) and the block |
|
522 | Just press enter and type -- (and press enter again) and the block | |
524 | will be what was just pasted. |
|
523 | will be what was just pasted. | |
525 |
|
524 | |||
526 | IPython statements (magics, shell escapes) are not supported (yet). |
|
525 | IPython statements (magics, shell escapes) are not supported (yet). | |
527 |
|
526 | |||
528 | See also |
|
527 | See also | |
529 | -------- |
|
528 | -------- | |
530 | paste: automatically pull code from clipboard. |
|
529 | paste: automatically pull code from clipboard. | |
531 |
|
530 | |||
532 | Examples |
|
531 | Examples | |
533 | -------- |
|
532 | -------- | |
534 | :: |
|
533 | :: | |
535 |
|
534 | |||
536 | In [8]: %cpaste |
|
535 | In [8]: %cpaste | |
537 | Pasting code; enter '--' alone on the line to stop. |
|
536 | Pasting code; enter '--' alone on the line to stop. | |
538 | :>>> a = ["world!", "Hello"] |
|
537 | :>>> a = ["world!", "Hello"] | |
539 | :>>> print " ".join(sorted(a)) |
|
538 | :>>> print " ".join(sorted(a)) | |
540 | :-- |
|
539 | :-- | |
541 | Hello world! |
|
540 | Hello world! | |
542 | """ |
|
541 | """ | |
543 |
|
542 | |||
544 | opts,args = self.parse_options(parameter_s,'rs:',mode='string') |
|
543 | opts,args = self.parse_options(parameter_s,'rs:',mode='string') | |
545 | par = args.strip() |
|
544 | par = args.strip() | |
546 | if opts.has_key('r'): |
|
545 | if opts.has_key('r'): | |
547 | self._rerun_pasted() |
|
546 | self._rerun_pasted() | |
548 | return |
|
547 | return | |
549 |
|
548 | |||
550 | sentinel = opts.get('s','--') |
|
549 | sentinel = opts.get('s','--') | |
551 |
|
550 | |||
552 | block = self._strip_pasted_lines_for_code( |
|
551 | block = self._strip_pasted_lines_for_code( | |
553 | self._get_pasted_lines(sentinel)) |
|
552 | self._get_pasted_lines(sentinel)) | |
554 |
|
553 | |||
555 | self._execute_block(block, par) |
|
554 | self._execute_block(block, par) | |
556 |
|
555 | |||
557 | def magic_paste(self, parameter_s=''): |
|
556 | def magic_paste(self, parameter_s=''): | |
558 | """Paste & execute a pre-formatted code block from clipboard. |
|
557 | """Paste & execute a pre-formatted code block from clipboard. | |
559 |
|
558 | |||
560 | The text is pulled directly from the clipboard without user |
|
559 | The text is pulled directly from the clipboard without user | |
561 | intervention and printed back on the screen before execution (unless |
|
560 | intervention and printed back on the screen before execution (unless | |
562 | the -q flag is given to force quiet mode). |
|
561 | the -q flag is given to force quiet mode). | |
563 |
|
562 | |||
564 | The block is dedented prior to execution to enable execution of method |
|
563 | The block is dedented prior to execution to enable execution of method | |
565 | definitions. '>' and '+' characters at the beginning of a line are |
|
564 | definitions. '>' and '+' characters at the beginning of a line are | |
566 | ignored, to allow pasting directly from e-mails, diff files and |
|
565 | ignored, to allow pasting directly from e-mails, diff files and | |
567 | doctests (the '...' continuation prompt is also stripped). The |
|
566 | doctests (the '...' continuation prompt is also stripped). The | |
568 | executed block is also assigned to variable named 'pasted_block' for |
|
567 | executed block is also assigned to variable named 'pasted_block' for | |
569 | later editing with '%edit pasted_block'. |
|
568 | later editing with '%edit pasted_block'. | |
570 |
|
569 | |||
571 | You can also pass a variable name as an argument, e.g. '%paste foo'. |
|
570 | You can also pass a variable name as an argument, e.g. '%paste foo'. | |
572 | This assigns the pasted block to variable 'foo' as string, without |
|
571 | This assigns the pasted block to variable 'foo' as string, without | |
573 | dedenting or executing it (preceding >>> and + is still stripped) |
|
572 | dedenting or executing it (preceding >>> and + is still stripped) | |
574 |
|
573 | |||
575 | Options |
|
574 | Options | |
576 | ------- |
|
575 | ------- | |
577 |
|
576 | |||
578 | -r: re-executes the block previously entered by cpaste. |
|
577 | -r: re-executes the block previously entered by cpaste. | |
579 |
|
578 | |||
580 | -q: quiet mode: do not echo the pasted text back to the terminal. |
|
579 | -q: quiet mode: do not echo the pasted text back to the terminal. | |
581 |
|
580 | |||
582 | IPython statements (magics, shell escapes) are not supported (yet). |
|
581 | IPython statements (magics, shell escapes) are not supported (yet). | |
583 |
|
582 | |||
584 | See also |
|
583 | See also | |
585 | -------- |
|
584 | -------- | |
586 | cpaste: manually paste code into terminal until you mark its end. |
|
585 | cpaste: manually paste code into terminal until you mark its end. | |
587 | """ |
|
586 | """ | |
588 | opts,args = self.parse_options(parameter_s,'rq',mode='string') |
|
587 | opts,args = self.parse_options(parameter_s,'rq',mode='string') | |
589 | par = args.strip() |
|
588 | par = args.strip() | |
590 | if opts.has_key('r'): |
|
589 | if opts.has_key('r'): | |
591 | self._rerun_pasted() |
|
590 | self._rerun_pasted() | |
592 | return |
|
591 | return | |
593 |
|
592 | |||
594 | text = self.shell.hooks.clipboard_get() |
|
593 | text = self.shell.hooks.clipboard_get() | |
595 | block = self._strip_pasted_lines_for_code(text.splitlines()) |
|
594 | block = self._strip_pasted_lines_for_code(text.splitlines()) | |
596 |
|
595 | |||
597 | # By default, echo back to terminal unless quiet mode is requested |
|
596 | # By default, echo back to terminal unless quiet mode is requested | |
598 | if not opts.has_key('q'): |
|
597 | if not opts.has_key('q'): | |
599 | write = self.shell.write |
|
598 | write = self.shell.write | |
600 | write(self.shell.pycolorize(block)) |
|
599 | write(self.shell.pycolorize(block)) | |
601 | if not block.endswith('\n'): |
|
600 | if not block.endswith('\n'): | |
602 | write('\n') |
|
601 | write('\n') | |
603 | write("## -- End pasted text --\n") |
|
602 | write("## -- End pasted text --\n") | |
604 |
|
603 | |||
605 | self._execute_block(block, par) |
|
604 | self._execute_block(block, par) | |
606 |
|
605 | |||
607 | def showindentationerror(self): |
|
606 | def showindentationerror(self): | |
608 | super(TerminalInteractiveShell, self).showindentationerror() |
|
607 | super(TerminalInteractiveShell, self).showindentationerror() | |
609 | print("If you want to paste code into IPython, try the %paste magic function.") |
|
608 | print("If you want to paste code into IPython, try the %paste magic function.") | |
610 |
|
609 | |||
611 |
|
610 | |||
612 | InteractiveShellABC.register(TerminalInteractiveShell) |
|
611 | InteractiveShellABC.register(TerminalInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now