Show More
@@ -1,422 +1,432 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | A mixin for :class:`~IPython.core.application.Application` classes that |
|
3 | A mixin for :class:`~IPython.core.application.Application` classes that | |
4 | launch InteractiveShell instances, load extensions, etc. |
|
4 | launch InteractiveShell instances, load extensions, etc. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | # Copyright (c) IPython Development Team. |
|
7 | # Copyright (c) IPython Development Team. | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 |
|
9 | |||
10 | from __future__ import absolute_import |
|
10 | from __future__ import absolute_import | |
11 | from __future__ import print_function |
|
11 | from __future__ import print_function | |
12 |
|
12 | |||
13 | import glob |
|
13 | import glob | |
14 | import os |
|
14 | import os | |
15 | import sys |
|
15 | import sys | |
16 |
|
16 | |||
17 | from IPython.config.application import boolean_flag |
|
17 | from IPython.config.application import boolean_flag | |
18 | from IPython.config.configurable import Configurable |
|
18 | from IPython.config.configurable import Configurable | |
19 | from IPython.config.loader import Config |
|
19 | from IPython.config.loader import Config | |
20 | from IPython.core import pylabtools |
|
20 | from IPython.core import pylabtools | |
21 | from IPython.utils import py3compat |
|
21 | from IPython.utils import py3compat | |
22 | from IPython.utils.contexts import preserve_keys |
|
22 | from IPython.utils.contexts import preserve_keys | |
23 | from IPython.utils.path import filefind |
|
23 | from IPython.utils.path import filefind | |
24 | from IPython.utils.traitlets import ( |
|
24 | from IPython.utils.traitlets import ( | |
25 | Unicode, Instance, List, Bool, CaselessStrEnum |
|
25 | Unicode, Instance, List, Bool, CaselessStrEnum | |
26 | ) |
|
26 | ) | |
27 | from IPython.lib.inputhook import guis |
|
27 | from IPython.lib.inputhook import guis | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Aliases and Flags |
|
30 | # Aliases and Flags | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | gui_keys = tuple(sorted([ key for key in guis if key is not None ])) |
|
33 | gui_keys = tuple(sorted([ key for key in guis if key is not None ])) | |
34 |
|
34 | |||
35 | backend_keys = sorted(pylabtools.backends.keys()) |
|
35 | backend_keys = sorted(pylabtools.backends.keys()) | |
36 | backend_keys.insert(0, 'auto') |
|
36 | backend_keys.insert(0, 'auto') | |
37 |
|
37 | |||
38 | shell_flags = {} |
|
38 | shell_flags = {} | |
39 |
|
39 | |||
40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) |
|
40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) | |
41 | addflag('autoindent', 'InteractiveShell.autoindent', |
|
41 | addflag('autoindent', 'InteractiveShell.autoindent', | |
42 | 'Turn on autoindenting.', 'Turn off autoindenting.' |
|
42 | 'Turn on autoindenting.', 'Turn off autoindenting.' | |
43 | ) |
|
43 | ) | |
44 | addflag('automagic', 'InteractiveShell.automagic', |
|
44 | addflag('automagic', 'InteractiveShell.automagic', | |
45 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
45 | """Turn on the auto calling of magic commands. Type %%magic at the | |
46 | IPython prompt for more information.""", |
|
46 | IPython prompt for more information.""", | |
47 | 'Turn off the auto calling of magic commands.' |
|
47 | 'Turn off the auto calling of magic commands.' | |
48 | ) |
|
48 | ) | |
49 | addflag('pdb', 'InteractiveShell.pdb', |
|
49 | addflag('pdb', 'InteractiveShell.pdb', | |
50 | "Enable auto calling the pdb debugger after every exception.", |
|
50 | "Enable auto calling the pdb debugger after every exception.", | |
51 | "Disable auto calling the pdb debugger after every exception." |
|
51 | "Disable auto calling the pdb debugger after every exception." | |
52 | ) |
|
52 | ) | |
53 | # pydb flag doesn't do any config, as core.debugger switches on import, |
|
53 | # pydb flag doesn't do any config, as core.debugger switches on import, | |
54 | # which is before parsing. This just allows the flag to be passed. |
|
54 | # which is before parsing. This just allows the flag to be passed. | |
55 | shell_flags.update(dict( |
|
55 | shell_flags.update(dict( | |
56 | pydb = ({}, |
|
56 | pydb = ({}, | |
57 | """Use the third party 'pydb' package as debugger, instead of pdb. |
|
57 | """Use the third party 'pydb' package as debugger, instead of pdb. | |
58 | Requires that pydb is installed.""" |
|
58 | Requires that pydb is installed.""" | |
59 | ) |
|
59 | ) | |
60 | )) |
|
60 | )) | |
61 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
61 | addflag('pprint', 'PlainTextFormatter.pprint', | |
62 | "Enable auto pretty printing of results.", |
|
62 | "Enable auto pretty printing of results.", | |
63 | "Disable auto pretty printing of results." |
|
63 | "Disable auto pretty printing of results." | |
64 | ) |
|
64 | ) | |
65 | addflag('color-info', 'InteractiveShell.color_info', |
|
65 | addflag('color-info', 'InteractiveShell.color_info', | |
66 | """IPython can display information about objects via a set of functions, |
|
66 | """IPython can display information about objects via a set of functions, | |
67 | and optionally can use colors for this, syntax highlighting |
|
67 | and optionally can use colors for this, syntax highlighting | |
68 | source code and various other elements. This is on by default, but can cause |
|
68 | source code and various other elements. This is on by default, but can cause | |
69 | problems with some pagers. If you see such problems, you can disable the |
|
69 | problems with some pagers. If you see such problems, you can disable the | |
70 | colours.""", |
|
70 | colours.""", | |
71 | "Disable using colors for info related things." |
|
71 | "Disable using colors for info related things." | |
72 | ) |
|
72 | ) | |
73 | addflag('deep-reload', 'InteractiveShell.deep_reload', |
|
73 | addflag('deep-reload', 'InteractiveShell.deep_reload', | |
74 | """Enable deep (recursive) reloading by default. IPython can use the |
|
74 | """Enable deep (recursive) reloading by default. IPython can use the | |
75 | deep_reload module which reloads changes in modules recursively (it |
|
75 | deep_reload module which reloads changes in modules recursively (it | |
76 | replaces the reload() function, so you don't need to change anything to |
|
76 | replaces the reload() function, so you don't need to change anything to | |
77 | use it). deep_reload() forces a full reload of modules whose code may |
|
77 | use it). deep_reload() forces a full reload of modules whose code may | |
78 | have changed, which the default reload() function does not. When |
|
78 | have changed, which the default reload() function does not. When | |
79 | deep_reload is off, IPython will use the normal reload(), but |
|
79 | deep_reload is off, IPython will use the normal reload(), but | |
80 | deep_reload will still be available as dreload(). This feature is off |
|
80 | deep_reload will still be available as dreload(). This feature is off | |
81 | by default [which means that you have both normal reload() and |
|
81 | by default [which means that you have both normal reload() and | |
82 | dreload()].""", |
|
82 | dreload()].""", | |
83 | "Disable deep (recursive) reloading by default." |
|
83 | "Disable deep (recursive) reloading by default." | |
84 | ) |
|
84 | ) | |
85 | nosep_config = Config() |
|
85 | nosep_config = Config() | |
86 | nosep_config.InteractiveShell.separate_in = '' |
|
86 | nosep_config.InteractiveShell.separate_in = '' | |
87 | nosep_config.InteractiveShell.separate_out = '' |
|
87 | nosep_config.InteractiveShell.separate_out = '' | |
88 | nosep_config.InteractiveShell.separate_out2 = '' |
|
88 | nosep_config.InteractiveShell.separate_out2 = '' | |
89 |
|
89 | |||
90 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
90 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") | |
91 | shell_flags['pylab'] = ( |
|
91 | shell_flags['pylab'] = ( | |
92 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, |
|
92 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, | |
93 | """Pre-load matplotlib and numpy for interactive use with |
|
93 | """Pre-load matplotlib and numpy for interactive use with | |
94 | the default matplotlib backend.""" |
|
94 | the default matplotlib backend.""" | |
95 | ) |
|
95 | ) | |
96 | shell_flags['matplotlib'] = ( |
|
96 | shell_flags['matplotlib'] = ( | |
97 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, |
|
97 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, | |
98 | """Configure matplotlib for interactive use with |
|
98 | """Configure matplotlib for interactive use with | |
99 | the default matplotlib backend.""" |
|
99 | the default matplotlib backend.""" | |
100 | ) |
|
100 | ) | |
101 |
|
101 | |||
102 | # it's possible we don't want short aliases for *all* of these: |
|
102 | # it's possible we don't want short aliases for *all* of these: | |
103 | shell_aliases = dict( |
|
103 | shell_aliases = dict( | |
104 | autocall='InteractiveShell.autocall', |
|
104 | autocall='InteractiveShell.autocall', | |
105 | colors='InteractiveShell.colors', |
|
105 | colors='InteractiveShell.colors', | |
106 | logfile='InteractiveShell.logfile', |
|
106 | logfile='InteractiveShell.logfile', | |
107 | logappend='InteractiveShell.logappend', |
|
107 | logappend='InteractiveShell.logappend', | |
108 | c='InteractiveShellApp.code_to_run', |
|
108 | c='InteractiveShellApp.code_to_run', | |
109 | m='InteractiveShellApp.module_to_run', |
|
109 | m='InteractiveShellApp.module_to_run', | |
110 | ext='InteractiveShellApp.extra_extension', |
|
110 | ext='InteractiveShellApp.extra_extension', | |
111 | gui='InteractiveShellApp.gui', |
|
111 | gui='InteractiveShellApp.gui', | |
112 | pylab='InteractiveShellApp.pylab', |
|
112 | pylab='InteractiveShellApp.pylab', | |
113 | matplotlib='InteractiveShellApp.matplotlib', |
|
113 | matplotlib='InteractiveShellApp.matplotlib', | |
114 | ) |
|
114 | ) | |
115 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' |
|
115 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' | |
116 |
|
116 | |||
117 | #----------------------------------------------------------------------------- |
|
117 | #----------------------------------------------------------------------------- | |
118 | # Main classes and functions |
|
118 | # Main classes and functions | |
119 | #----------------------------------------------------------------------------- |
|
119 | #----------------------------------------------------------------------------- | |
120 |
|
120 | |||
121 | class InteractiveShellApp(Configurable): |
|
121 | class InteractiveShellApp(Configurable): | |
122 | """A Mixin for applications that start InteractiveShell instances. |
|
122 | """A Mixin for applications that start InteractiveShell instances. | |
123 |
|
123 | |||
124 | Provides configurables for loading extensions and executing files |
|
124 | Provides configurables for loading extensions and executing files | |
125 | as part of configuring a Shell environment. |
|
125 | as part of configuring a Shell environment. | |
126 |
|
126 | |||
127 | The following methods should be called by the :meth:`initialize` method |
|
127 | The following methods should be called by the :meth:`initialize` method | |
128 | of the subclass: |
|
128 | of the subclass: | |
129 |
|
129 | |||
130 | - :meth:`init_path` |
|
130 | - :meth:`init_path` | |
131 | - :meth:`init_shell` (to be implemented by the subclass) |
|
131 | - :meth:`init_shell` (to be implemented by the subclass) | |
132 | - :meth:`init_gui_pylab` |
|
132 | - :meth:`init_gui_pylab` | |
133 | - :meth:`init_extensions` |
|
133 | - :meth:`init_extensions` | |
134 | - :meth:`init_code` |
|
134 | - :meth:`init_code` | |
135 | """ |
|
135 | """ | |
136 | extensions = List(Unicode, config=True, |
|
136 | extensions = List(Unicode, config=True, | |
137 | help="A list of dotted module names of IPython extensions to load." |
|
137 | help="A list of dotted module names of IPython extensions to load." | |
138 | ) |
|
138 | ) | |
139 | extra_extension = Unicode('', config=True, |
|
139 | extra_extension = Unicode('', config=True, | |
140 | help="dotted module name of an IPython extension to load." |
|
140 | help="dotted module name of an IPython extension to load." | |
141 | ) |
|
141 | ) | |
142 |
|
142 | |||
|
143 | reraise_ipython_extension_failures = Bool( | |||
|
144 | False, | |||
|
145 | config=True, | |||
|
146 | help="If True, exit on failure to load any extensions.", | |||
|
147 | ) | |||
|
148 | ||||
143 | # Extensions that are always loaded (not configurable) |
|
149 | # Extensions that are always loaded (not configurable) | |
144 | default_extensions = List(Unicode, [u'storemagic'], config=False) |
|
150 | default_extensions = List(Unicode, [u'storemagic'], config=False) | |
145 |
|
151 | |||
146 | hide_initial_ns = Bool(True, config=True, |
|
152 | hide_initial_ns = Bool(True, config=True, | |
147 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) |
|
153 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) | |
148 | be hidden from tools like %who?""" |
|
154 | be hidden from tools like %who?""" | |
149 | ) |
|
155 | ) | |
150 |
|
156 | |||
151 | exec_files = List(Unicode, config=True, |
|
157 | exec_files = List(Unicode, config=True, | |
152 | help="""List of files to run at IPython startup.""" |
|
158 | help="""List of files to run at IPython startup.""" | |
153 | ) |
|
159 | ) | |
154 | exec_PYTHONSTARTUP = Bool(True, config=True, |
|
160 | exec_PYTHONSTARTUP = Bool(True, config=True, | |
155 | help="""Run the file referenced by the PYTHONSTARTUP environment |
|
161 | help="""Run the file referenced by the PYTHONSTARTUP environment | |
156 | variable at IPython startup.""" |
|
162 | variable at IPython startup.""" | |
157 | ) |
|
163 | ) | |
158 | file_to_run = Unicode('', config=True, |
|
164 | file_to_run = Unicode('', config=True, | |
159 | help="""A file to be run""") |
|
165 | help="""A file to be run""") | |
160 |
|
166 | |||
161 | exec_lines = List(Unicode, config=True, |
|
167 | exec_lines = List(Unicode, config=True, | |
162 | help="""lines of code to run at IPython startup.""" |
|
168 | help="""lines of code to run at IPython startup.""" | |
163 | ) |
|
169 | ) | |
164 | code_to_run = Unicode('', config=True, |
|
170 | code_to_run = Unicode('', config=True, | |
165 | help="Execute the given command string." |
|
171 | help="Execute the given command string." | |
166 | ) |
|
172 | ) | |
167 | module_to_run = Unicode('', config=True, |
|
173 | module_to_run = Unicode('', config=True, | |
168 | help="Run the module as a script." |
|
174 | help="Run the module as a script." | |
169 | ) |
|
175 | ) | |
170 | gui = CaselessStrEnum(gui_keys, config=True, |
|
176 | gui = CaselessStrEnum(gui_keys, config=True, | |
171 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) |
|
177 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) | |
172 | ) |
|
178 | ) | |
173 | matplotlib = CaselessStrEnum(backend_keys, |
|
179 | matplotlib = CaselessStrEnum(backend_keys, | |
174 | config=True, |
|
180 | config=True, | |
175 | help="""Configure matplotlib for interactive use with |
|
181 | help="""Configure matplotlib for interactive use with | |
176 | the default matplotlib backend.""" |
|
182 | the default matplotlib backend.""" | |
177 | ) |
|
183 | ) | |
178 | pylab = CaselessStrEnum(backend_keys, |
|
184 | pylab = CaselessStrEnum(backend_keys, | |
179 | config=True, |
|
185 | config=True, | |
180 | help="""Pre-load matplotlib and numpy for interactive use, |
|
186 | help="""Pre-load matplotlib and numpy for interactive use, | |
181 | selecting a particular matplotlib backend and loop integration. |
|
187 | selecting a particular matplotlib backend and loop integration. | |
182 | """ |
|
188 | """ | |
183 | ) |
|
189 | ) | |
184 | pylab_import_all = Bool(True, config=True, |
|
190 | pylab_import_all = Bool(True, config=True, | |
185 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. |
|
191 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. | |
186 | and an ``import *`` is done from numpy and pylab, when using pylab mode. |
|
192 | and an ``import *`` is done from numpy and pylab, when using pylab mode. | |
187 |
|
193 | |||
188 | When False, pylab mode should not import any names into the user namespace. |
|
194 | When False, pylab mode should not import any names into the user namespace. | |
189 | """ |
|
195 | """ | |
190 | ) |
|
196 | ) | |
191 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
197 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
192 |
|
198 | |||
193 | user_ns = Instance(dict, args=None, allow_none=True) |
|
199 | user_ns = Instance(dict, args=None, allow_none=True) | |
194 | def _user_ns_changed(self, name, old, new): |
|
200 | def _user_ns_changed(self, name, old, new): | |
195 | if self.shell is not None: |
|
201 | if self.shell is not None: | |
196 | self.shell.user_ns = new |
|
202 | self.shell.user_ns = new | |
197 | self.shell.init_user_ns() |
|
203 | self.shell.init_user_ns() | |
198 |
|
204 | |||
199 | def init_path(self): |
|
205 | def init_path(self): | |
200 | """Add current working directory, '', to sys.path""" |
|
206 | """Add current working directory, '', to sys.path""" | |
201 | if sys.path[0] != '': |
|
207 | if sys.path[0] != '': | |
202 | sys.path.insert(0, '') |
|
208 | sys.path.insert(0, '') | |
203 |
|
209 | |||
204 | def init_shell(self): |
|
210 | def init_shell(self): | |
205 | raise NotImplementedError("Override in subclasses") |
|
211 | raise NotImplementedError("Override in subclasses") | |
206 |
|
212 | |||
207 | def init_gui_pylab(self): |
|
213 | def init_gui_pylab(self): | |
208 | """Enable GUI event loop integration, taking pylab into account.""" |
|
214 | """Enable GUI event loop integration, taking pylab into account.""" | |
209 | enable = False |
|
215 | enable = False | |
210 | shell = self.shell |
|
216 | shell = self.shell | |
211 | if self.pylab: |
|
217 | if self.pylab: | |
212 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) |
|
218 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) | |
213 | key = self.pylab |
|
219 | key = self.pylab | |
214 | elif self.matplotlib: |
|
220 | elif self.matplotlib: | |
215 | enable = shell.enable_matplotlib |
|
221 | enable = shell.enable_matplotlib | |
216 | key = self.matplotlib |
|
222 | key = self.matplotlib | |
217 | elif self.gui: |
|
223 | elif self.gui: | |
218 | enable = shell.enable_gui |
|
224 | enable = shell.enable_gui | |
219 | key = self.gui |
|
225 | key = self.gui | |
220 |
|
226 | |||
221 | if not enable: |
|
227 | if not enable: | |
222 | return |
|
228 | return | |
223 |
|
229 | |||
224 | try: |
|
230 | try: | |
225 | r = enable(key) |
|
231 | r = enable(key) | |
226 | except ImportError: |
|
232 | except ImportError: | |
227 | self.log.warn("Eventloop or matplotlib integration failed. Is matplotlib installed?") |
|
233 | self.log.warn("Eventloop or matplotlib integration failed. Is matplotlib installed?") | |
228 | self.shell.showtraceback() |
|
234 | self.shell.showtraceback() | |
229 | return |
|
235 | return | |
230 | except Exception: |
|
236 | except Exception: | |
231 | self.log.warn("GUI event loop or pylab initialization failed") |
|
237 | self.log.warn("GUI event loop or pylab initialization failed") | |
232 | self.shell.showtraceback() |
|
238 | self.shell.showtraceback() | |
233 | return |
|
239 | return | |
234 |
|
240 | |||
235 | if isinstance(r, tuple): |
|
241 | if isinstance(r, tuple): | |
236 | gui, backend = r[:2] |
|
242 | gui, backend = r[:2] | |
237 | self.log.info("Enabling GUI event loop integration, " |
|
243 | self.log.info("Enabling GUI event loop integration, " | |
238 | "eventloop=%s, matplotlib=%s", gui, backend) |
|
244 | "eventloop=%s, matplotlib=%s", gui, backend) | |
239 | if key == "auto": |
|
245 | if key == "auto": | |
240 | print("Using matplotlib backend: %s" % backend) |
|
246 | print("Using matplotlib backend: %s" % backend) | |
241 | else: |
|
247 | else: | |
242 | gui = r |
|
248 | gui = r | |
243 | self.log.info("Enabling GUI event loop integration, " |
|
249 | self.log.info("Enabling GUI event loop integration, " | |
244 | "eventloop=%s", gui) |
|
250 | "eventloop=%s", gui) | |
245 |
|
251 | |||
246 | def init_extensions(self): |
|
252 | def init_extensions(self): | |
247 | """Load all IPython extensions in IPythonApp.extensions. |
|
253 | """Load all IPython extensions in IPythonApp.extensions. | |
248 |
|
254 | |||
249 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
255 | This uses the :meth:`ExtensionManager.load_extensions` to load all | |
250 | the extensions listed in ``self.extensions``. |
|
256 | the extensions listed in ``self.extensions``. | |
251 | """ |
|
257 | """ | |
252 | try: |
|
258 | try: | |
253 | self.log.debug("Loading IPython extensions...") |
|
259 | self.log.debug("Loading IPython extensions...") | |
254 | extensions = self.default_extensions + self.extensions |
|
260 | extensions = self.default_extensions + self.extensions | |
255 | if self.extra_extension: |
|
261 | if self.extra_extension: | |
256 | extensions.append(self.extra_extension) |
|
262 | extensions.append(self.extra_extension) | |
257 | for ext in extensions: |
|
263 | for ext in extensions: | |
258 | try: |
|
264 | try: | |
259 | self.log.info("Loading IPython extension: %s" % ext) |
|
265 | self.log.info("Loading IPython extension: %s" % ext) | |
260 | self.shell.extension_manager.load_extension(ext) |
|
266 | self.shell.extension_manager.load_extension(ext) | |
261 | except: |
|
267 | except: | |
|
268 | if self.exit_on_extension_load_failure: | |||
|
269 | raise | |||
262 | msg = ("Error in loading extension: {ext}\n" |
|
270 | msg = ("Error in loading extension: {ext}\n" | |
263 | "Check your config files in {location}".format( |
|
271 | "Check your config files in {location}".format( | |
264 | ext=ext, |
|
272 | ext=ext, | |
265 | location=self.profile_dir.location |
|
273 | location=self.profile_dir.location | |
266 | )) |
|
274 | )) | |
267 | self.log.warn(msg, exc_info=True) |
|
275 | self.log.warn(msg, exc_info=True) | |
268 | except: |
|
276 | except: | |
|
277 | if self.exit_on_extension_load_failure: | |||
|
278 | raise | |||
269 | self.log.warn("Unknown error in loading extensions:", exc_info=True) |
|
279 | self.log.warn("Unknown error in loading extensions:", exc_info=True) | |
270 |
|
280 | |||
271 | def init_code(self): |
|
281 | def init_code(self): | |
272 | """run the pre-flight code, specified via exec_lines""" |
|
282 | """run the pre-flight code, specified via exec_lines""" | |
273 | self._run_startup_files() |
|
283 | self._run_startup_files() | |
274 | self._run_exec_lines() |
|
284 | self._run_exec_lines() | |
275 | self._run_exec_files() |
|
285 | self._run_exec_files() | |
276 |
|
286 | |||
277 | # Hide variables defined here from %who etc. |
|
287 | # Hide variables defined here from %who etc. | |
278 | if self.hide_initial_ns: |
|
288 | if self.hide_initial_ns: | |
279 | self.shell.user_ns_hidden.update(self.shell.user_ns) |
|
289 | self.shell.user_ns_hidden.update(self.shell.user_ns) | |
280 |
|
290 | |||
281 | # command-line execution (ipython -i script.py, ipython -m module) |
|
291 | # command-line execution (ipython -i script.py, ipython -m module) | |
282 | # should *not* be excluded from %whos |
|
292 | # should *not* be excluded from %whos | |
283 | self._run_cmd_line_code() |
|
293 | self._run_cmd_line_code() | |
284 | self._run_module() |
|
294 | self._run_module() | |
285 |
|
295 | |||
286 | # flush output, so itwon't be attached to the first cell |
|
296 | # flush output, so itwon't be attached to the first cell | |
287 | sys.stdout.flush() |
|
297 | sys.stdout.flush() | |
288 | sys.stderr.flush() |
|
298 | sys.stderr.flush() | |
289 |
|
299 | |||
290 | def _run_exec_lines(self): |
|
300 | def _run_exec_lines(self): | |
291 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
301 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" | |
292 | if not self.exec_lines: |
|
302 | if not self.exec_lines: | |
293 | return |
|
303 | return | |
294 | try: |
|
304 | try: | |
295 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
305 | self.log.debug("Running code from IPythonApp.exec_lines...") | |
296 | for line in self.exec_lines: |
|
306 | for line in self.exec_lines: | |
297 | try: |
|
307 | try: | |
298 | self.log.info("Running code in user namespace: %s" % |
|
308 | self.log.info("Running code in user namespace: %s" % | |
299 | line) |
|
309 | line) | |
300 | self.shell.run_cell(line, store_history=False) |
|
310 | self.shell.run_cell(line, store_history=False) | |
301 | except: |
|
311 | except: | |
302 | self.log.warn("Error in executing line in user " |
|
312 | self.log.warn("Error in executing line in user " | |
303 | "namespace: %s" % line) |
|
313 | "namespace: %s" % line) | |
304 | self.shell.showtraceback() |
|
314 | self.shell.showtraceback() | |
305 | except: |
|
315 | except: | |
306 | self.log.warn("Unknown error in handling IPythonApp.exec_lines:") |
|
316 | self.log.warn("Unknown error in handling IPythonApp.exec_lines:") | |
307 | self.shell.showtraceback() |
|
317 | self.shell.showtraceback() | |
308 |
|
318 | |||
309 | def _exec_file(self, fname, shell_futures=False): |
|
319 | def _exec_file(self, fname, shell_futures=False): | |
310 | try: |
|
320 | try: | |
311 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
321 | full_filename = filefind(fname, [u'.', self.ipython_dir]) | |
312 | except IOError as e: |
|
322 | except IOError as e: | |
313 | self.log.warn("File not found: %r"%fname) |
|
323 | self.log.warn("File not found: %r"%fname) | |
314 | return |
|
324 | return | |
315 | # Make sure that the running script gets a proper sys.argv as if it |
|
325 | # Make sure that the running script gets a proper sys.argv as if it | |
316 | # were run from a system shell. |
|
326 | # were run from a system shell. | |
317 | save_argv = sys.argv |
|
327 | save_argv = sys.argv | |
318 | sys.argv = [full_filename] + self.extra_args[1:] |
|
328 | sys.argv = [full_filename] + self.extra_args[1:] | |
319 | # protect sys.argv from potential unicode strings on Python 2: |
|
329 | # protect sys.argv from potential unicode strings on Python 2: | |
320 | if not py3compat.PY3: |
|
330 | if not py3compat.PY3: | |
321 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
331 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |
322 | try: |
|
332 | try: | |
323 | if os.path.isfile(full_filename): |
|
333 | if os.path.isfile(full_filename): | |
324 | self.log.info("Running file in user namespace: %s" % |
|
334 | self.log.info("Running file in user namespace: %s" % | |
325 | full_filename) |
|
335 | full_filename) | |
326 | # Ensure that __file__ is always defined to match Python |
|
336 | # Ensure that __file__ is always defined to match Python | |
327 | # behavior. |
|
337 | # behavior. | |
328 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
338 | with preserve_keys(self.shell.user_ns, '__file__'): | |
329 | self.shell.user_ns['__file__'] = fname |
|
339 | self.shell.user_ns['__file__'] = fname | |
330 | if full_filename.endswith('.ipy'): |
|
340 | if full_filename.endswith('.ipy'): | |
331 | self.shell.safe_execfile_ipy(full_filename, |
|
341 | self.shell.safe_execfile_ipy(full_filename, | |
332 | shell_futures=shell_futures) |
|
342 | shell_futures=shell_futures) | |
333 | else: |
|
343 | else: | |
334 | # default to python, even without extension |
|
344 | # default to python, even without extension | |
335 | self.shell.safe_execfile(full_filename, |
|
345 | self.shell.safe_execfile(full_filename, | |
336 | self.shell.user_ns, |
|
346 | self.shell.user_ns, | |
337 | shell_futures=shell_futures) |
|
347 | shell_futures=shell_futures) | |
338 | finally: |
|
348 | finally: | |
339 | sys.argv = save_argv |
|
349 | sys.argv = save_argv | |
340 |
|
350 | |||
341 | def _run_startup_files(self): |
|
351 | def _run_startup_files(self): | |
342 | """Run files from profile startup directory""" |
|
352 | """Run files from profile startup directory""" | |
343 | startup_dir = self.profile_dir.startup_dir |
|
353 | startup_dir = self.profile_dir.startup_dir | |
344 | startup_files = [] |
|
354 | startup_files = [] | |
345 |
|
355 | |||
346 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ |
|
356 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ | |
347 | not (self.file_to_run or self.code_to_run or self.module_to_run): |
|
357 | not (self.file_to_run or self.code_to_run or self.module_to_run): | |
348 | python_startup = os.environ['PYTHONSTARTUP'] |
|
358 | python_startup = os.environ['PYTHONSTARTUP'] | |
349 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) |
|
359 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) | |
350 | try: |
|
360 | try: | |
351 | self._exec_file(python_startup) |
|
361 | self._exec_file(python_startup) | |
352 | except: |
|
362 | except: | |
353 | self.log.warn("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) |
|
363 | self.log.warn("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) | |
354 | self.shell.showtraceback() |
|
364 | self.shell.showtraceback() | |
355 | finally: |
|
365 | finally: | |
356 | # Many PYTHONSTARTUP files set up the readline completions, |
|
366 | # Many PYTHONSTARTUP files set up the readline completions, | |
357 | # but this is often at odds with IPython's own completions. |
|
367 | # but this is often at odds with IPython's own completions. | |
358 | # Do not allow PYTHONSTARTUP to set up readline. |
|
368 | # Do not allow PYTHONSTARTUP to set up readline. | |
359 | if self.shell.has_readline: |
|
369 | if self.shell.has_readline: | |
360 | self.shell.set_readline_completer() |
|
370 | self.shell.set_readline_completer() | |
361 |
|
371 | |||
362 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) |
|
372 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) | |
363 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) |
|
373 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) | |
364 | if not startup_files: |
|
374 | if not startup_files: | |
365 | return |
|
375 | return | |
366 |
|
376 | |||
367 | self.log.debug("Running startup files from %s...", startup_dir) |
|
377 | self.log.debug("Running startup files from %s...", startup_dir) | |
368 | try: |
|
378 | try: | |
369 | for fname in sorted(startup_files): |
|
379 | for fname in sorted(startup_files): | |
370 | self._exec_file(fname) |
|
380 | self._exec_file(fname) | |
371 | except: |
|
381 | except: | |
372 | self.log.warn("Unknown error in handling startup files:") |
|
382 | self.log.warn("Unknown error in handling startup files:") | |
373 | self.shell.showtraceback() |
|
383 | self.shell.showtraceback() | |
374 |
|
384 | |||
375 | def _run_exec_files(self): |
|
385 | def _run_exec_files(self): | |
376 | """Run files from IPythonApp.exec_files""" |
|
386 | """Run files from IPythonApp.exec_files""" | |
377 | if not self.exec_files: |
|
387 | if not self.exec_files: | |
378 | return |
|
388 | return | |
379 |
|
389 | |||
380 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
390 | self.log.debug("Running files in IPythonApp.exec_files...") | |
381 | try: |
|
391 | try: | |
382 | for fname in self.exec_files: |
|
392 | for fname in self.exec_files: | |
383 | self._exec_file(fname) |
|
393 | self._exec_file(fname) | |
384 | except: |
|
394 | except: | |
385 | self.log.warn("Unknown error in handling IPythonApp.exec_files:") |
|
395 | self.log.warn("Unknown error in handling IPythonApp.exec_files:") | |
386 | self.shell.showtraceback() |
|
396 | self.shell.showtraceback() | |
387 |
|
397 | |||
388 | def _run_cmd_line_code(self): |
|
398 | def _run_cmd_line_code(self): | |
389 | """Run code or file specified at the command-line""" |
|
399 | """Run code or file specified at the command-line""" | |
390 | if self.code_to_run: |
|
400 | if self.code_to_run: | |
391 | line = self.code_to_run |
|
401 | line = self.code_to_run | |
392 | try: |
|
402 | try: | |
393 | self.log.info("Running code given at command line (c=): %s" % |
|
403 | self.log.info("Running code given at command line (c=): %s" % | |
394 | line) |
|
404 | line) | |
395 | self.shell.run_cell(line, store_history=False) |
|
405 | self.shell.run_cell(line, store_history=False) | |
396 | except: |
|
406 | except: | |
397 | self.log.warn("Error in executing line in user namespace: %s" % |
|
407 | self.log.warn("Error in executing line in user namespace: %s" % | |
398 | line) |
|
408 | line) | |
399 | self.shell.showtraceback() |
|
409 | self.shell.showtraceback() | |
400 |
|
410 | |||
401 | # Like Python itself, ignore the second if the first of these is present |
|
411 | # Like Python itself, ignore the second if the first of these is present | |
402 | elif self.file_to_run: |
|
412 | elif self.file_to_run: | |
403 | fname = self.file_to_run |
|
413 | fname = self.file_to_run | |
404 | try: |
|
414 | try: | |
405 | self._exec_file(fname, shell_futures=True) |
|
415 | self._exec_file(fname, shell_futures=True) | |
406 | except: |
|
416 | except: | |
407 | self.log.warn("Error in executing file in user namespace: %s" % |
|
417 | self.log.warn("Error in executing file in user namespace: %s" % | |
408 | fname) |
|
418 | fname) | |
409 | self.shell.showtraceback() |
|
419 | self.shell.showtraceback() | |
410 |
|
420 | |||
411 | def _run_module(self): |
|
421 | def _run_module(self): | |
412 | """Run module specified at the command-line.""" |
|
422 | """Run module specified at the command-line.""" | |
413 | if self.module_to_run: |
|
423 | if self.module_to_run: | |
414 | # Make sure that the module gets a proper sys.argv as if it were |
|
424 | # Make sure that the module gets a proper sys.argv as if it were | |
415 | # run using `python -m`. |
|
425 | # run using `python -m`. | |
416 | save_argv = sys.argv |
|
426 | save_argv = sys.argv | |
417 | sys.argv = [sys.executable] + self.extra_args |
|
427 | sys.argv = [sys.executable] + self.extra_args | |
418 | try: |
|
428 | try: | |
419 | self.shell.safe_run_module(self.module_to_run, |
|
429 | self.shell.safe_run_module(self.module_to_run, | |
420 | self.shell.user_ns) |
|
430 | self.shell.user_ns) | |
421 | finally: |
|
431 | finally: | |
422 | sys.argv = save_argv |
|
432 | sys.argv = save_argv |
General Comments 0
You need to be logged in to leave comments.
Login now