Show More
@@ -1,2710 +1,2705 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Main IPython class.""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from __future__ import with_statement |
|
18 | 18 | from __future__ import absolute_import |
|
19 | 19 | |
|
20 | 20 | import __builtin__ as builtin_mod |
|
21 | 21 | import __future__ |
|
22 | 22 | import abc |
|
23 | 23 | import ast |
|
24 | 24 | import atexit |
|
25 | 25 | import codeop |
|
26 | 26 | import inspect |
|
27 | 27 | import os |
|
28 | 28 | import re |
|
29 | 29 | import sys |
|
30 | 30 | import tempfile |
|
31 | 31 | import types |
|
32 | 32 | try: |
|
33 | 33 | from contextlib import nested |
|
34 | 34 | except: |
|
35 | 35 | from IPython.utils.nested_context import nested |
|
36 | 36 | |
|
37 | 37 | from IPython.config.configurable import SingletonConfigurable |
|
38 | 38 | from IPython.core import debugger, oinspect |
|
39 | 39 | from IPython.core import history as ipcorehist |
|
40 | 40 | from IPython.core import page |
|
41 | 41 | from IPython.core import prefilter |
|
42 | 42 | from IPython.core import shadowns |
|
43 | 43 | from IPython.core import ultratb |
|
44 | 44 | from IPython.core.alias import AliasManager, AliasError |
|
45 | 45 | from IPython.core.autocall import ExitAutocall |
|
46 | 46 | from IPython.core.builtin_trap import BuiltinTrap |
|
47 | 47 | from IPython.core.compilerop import CachingCompiler |
|
48 | 48 | from IPython.core.display_trap import DisplayTrap |
|
49 | 49 | from IPython.core.displayhook import DisplayHook |
|
50 | 50 | from IPython.core.displaypub import DisplayPublisher |
|
51 | 51 | from IPython.core.error import TryNext, UsageError |
|
52 | 52 | from IPython.core.extensions import ExtensionManager |
|
53 | 53 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
54 | 54 | from IPython.core.formatters import DisplayFormatter |
|
55 | 55 | from IPython.core.history import HistoryManager |
|
56 | 56 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
57 | 57 | from IPython.core.logger import Logger |
|
58 | 58 | from IPython.core.macro import Macro |
|
59 | 59 | from IPython.core.magic import Magic |
|
60 | 60 | from IPython.core.payload import PayloadManager |
|
61 | 61 | from IPython.core.plugin import PluginManager |
|
62 | 62 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC |
|
63 | 63 | from IPython.core.profiledir import ProfileDir |
|
64 | 64 | from IPython.external.Itpl import ItplNS |
|
65 | 65 | from IPython.utils import PyColorize |
|
66 | 66 | from IPython.utils import io |
|
67 | 67 | from IPython.utils import py3compat |
|
68 | 68 | from IPython.utils.doctestreload import doctest_reload |
|
69 | 69 | from IPython.utils.io import ask_yes_no, rprint |
|
70 | 70 | from IPython.utils.ipstruct import Struct |
|
71 | 71 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
72 | 72 | from IPython.utils.pickleshare import PickleShareDB |
|
73 | 73 | from IPython.utils.process import system, getoutput |
|
74 | 74 | from IPython.utils.strdispatch import StrDispatch |
|
75 | 75 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
76 | 76 | from IPython.utils.text import (num_ini_spaces, format_screen, LSString, SList, |
|
77 | 77 | DollarFormatter) |
|
78 | 78 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, |
|
79 | 79 | List, Unicode, Instance, Type) |
|
80 | 80 | from IPython.utils.warn import warn, error, fatal |
|
81 | 81 | import IPython.core.hooks |
|
82 | 82 | |
|
83 | 83 | #----------------------------------------------------------------------------- |
|
84 | 84 | # Globals |
|
85 | 85 | #----------------------------------------------------------------------------- |
|
86 | 86 | |
|
87 | 87 | # compiled regexps for autoindent management |
|
88 | 88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
89 | 89 | |
|
90 | 90 | #----------------------------------------------------------------------------- |
|
91 | 91 | # Utilities |
|
92 | 92 | #----------------------------------------------------------------------------- |
|
93 | 93 | |
|
94 | 94 | def softspace(file, newvalue): |
|
95 | 95 | """Copied from code.py, to remove the dependency""" |
|
96 | 96 | |
|
97 | 97 | oldvalue = 0 |
|
98 | 98 | try: |
|
99 | 99 | oldvalue = file.softspace |
|
100 | 100 | except AttributeError: |
|
101 | 101 | pass |
|
102 | 102 | try: |
|
103 | 103 | file.softspace = newvalue |
|
104 | 104 | except (AttributeError, TypeError): |
|
105 | 105 | # "attribute-less object" or "read-only attributes" |
|
106 | 106 | pass |
|
107 | 107 | return oldvalue |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | def no_op(*a, **kw): pass |
|
111 | 111 | |
|
112 | 112 | class NoOpContext(object): |
|
113 | 113 | def __enter__(self): pass |
|
114 | 114 | def __exit__(self, type, value, traceback): pass |
|
115 | 115 | no_op_context = NoOpContext() |
|
116 | 116 | |
|
117 | 117 | class SpaceInInput(Exception): pass |
|
118 | 118 | |
|
119 | 119 | class Bunch: pass |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | def get_default_colors(): |
|
123 | 123 | if sys.platform=='darwin': |
|
124 | 124 | return "LightBG" |
|
125 | 125 | elif os.name=='nt': |
|
126 | 126 | return 'Linux' |
|
127 | 127 | else: |
|
128 | 128 | return 'Linux' |
|
129 | 129 | |
|
130 | 130 | |
|
131 | 131 | class SeparateUnicode(Unicode): |
|
132 | 132 | """A Unicode subclass to validate separate_in, separate_out, etc. |
|
133 | 133 | |
|
134 | 134 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. |
|
135 | 135 | """ |
|
136 | 136 | |
|
137 | 137 | def validate(self, obj, value): |
|
138 | 138 | if value == '0': value = '' |
|
139 | 139 | value = value.replace('\\n','\n') |
|
140 | 140 | return super(SeparateUnicode, self).validate(obj, value) |
|
141 | 141 | |
|
142 | 142 | |
|
143 | 143 | class ReadlineNoRecord(object): |
|
144 | 144 | """Context manager to execute some code, then reload readline history |
|
145 | 145 | so that interactive input to the code doesn't appear when pressing up.""" |
|
146 | 146 | def __init__(self, shell): |
|
147 | 147 | self.shell = shell |
|
148 | 148 | self._nested_level = 0 |
|
149 | 149 | |
|
150 | 150 | def __enter__(self): |
|
151 | 151 | if self._nested_level == 0: |
|
152 | 152 | try: |
|
153 | 153 | self.orig_length = self.current_length() |
|
154 | 154 | self.readline_tail = self.get_readline_tail() |
|
155 | 155 | except (AttributeError, IndexError): # Can fail with pyreadline |
|
156 | 156 | self.orig_length, self.readline_tail = 999999, [] |
|
157 | 157 | self._nested_level += 1 |
|
158 | 158 | |
|
159 | 159 | def __exit__(self, type, value, traceback): |
|
160 | 160 | self._nested_level -= 1 |
|
161 | 161 | if self._nested_level == 0: |
|
162 | 162 | # Try clipping the end if it's got longer |
|
163 | 163 | try: |
|
164 | 164 | e = self.current_length() - self.orig_length |
|
165 | 165 | if e > 0: |
|
166 | 166 | for _ in range(e): |
|
167 | 167 | self.shell.readline.remove_history_item(self.orig_length) |
|
168 | 168 | |
|
169 | 169 | # If it still doesn't match, just reload readline history. |
|
170 | 170 | if self.current_length() != self.orig_length \ |
|
171 | 171 | or self.get_readline_tail() != self.readline_tail: |
|
172 | 172 | self.shell.refill_readline_hist() |
|
173 | 173 | except (AttributeError, IndexError): |
|
174 | 174 | pass |
|
175 | 175 | # Returning False will cause exceptions to propagate |
|
176 | 176 | return False |
|
177 | 177 | |
|
178 | 178 | def current_length(self): |
|
179 | 179 | return self.shell.readline.get_current_history_length() |
|
180 | 180 | |
|
181 | 181 | def get_readline_tail(self, n=10): |
|
182 | 182 | """Get the last n items in readline history.""" |
|
183 | 183 | end = self.shell.readline.get_current_history_length() + 1 |
|
184 | 184 | start = max(end-n, 1) |
|
185 | 185 | ghi = self.shell.readline.get_history_item |
|
186 | 186 | return [ghi(x) for x in range(start, end)] |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | _autocall_help = """ |
|
190 | 190 | Make IPython automatically call any callable object even if |
|
191 | 191 | you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
192 | 192 | automatically. The value can be '0' to disable the feature, '1' for 'smart' |
|
193 | 193 | autocall, where it is not applied if there are no more arguments on the line, |
|
194 | 194 | and '2' for 'full' autocall, where all callable objects are automatically |
|
195 | 195 | called (even if no arguments are present). The default is '1'. |
|
196 | 196 | """ |
|
197 | 197 | |
|
198 | 198 | #----------------------------------------------------------------------------- |
|
199 | 199 | # Main IPython class |
|
200 | 200 | #----------------------------------------------------------------------------- |
|
201 | 201 | |
|
202 | 202 | class InteractiveShell(SingletonConfigurable, Magic): |
|
203 | 203 | """An enhanced, interactive shell for Python.""" |
|
204 | 204 | |
|
205 | 205 | _instance = None |
|
206 | 206 | |
|
207 | 207 | autocall = Enum((0,1,2), default_value=1, config=True, help= |
|
208 | 208 | """ |
|
209 | 209 | Make IPython automatically call any callable object even if you didn't |
|
210 | 210 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
211 | 211 | automatically. The value can be '0' to disable the feature, '1' for |
|
212 | 212 | 'smart' autocall, where it is not applied if there are no more |
|
213 | 213 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
214 | 214 | objects are automatically called (even if no arguments are present). |
|
215 | 215 | The default is '1'. |
|
216 | 216 | """ |
|
217 | 217 | ) |
|
218 | 218 | # TODO: remove all autoindent logic and put into frontends. |
|
219 | 219 | # We can't do this yet because even runlines uses the autoindent. |
|
220 | 220 | autoindent = CBool(True, config=True, help= |
|
221 | 221 | """ |
|
222 | 222 | Autoindent IPython code entered interactively. |
|
223 | 223 | """ |
|
224 | 224 | ) |
|
225 | 225 | automagic = CBool(True, config=True, help= |
|
226 | 226 | """ |
|
227 | 227 | Enable magic commands to be called without the leading %. |
|
228 | 228 | """ |
|
229 | 229 | ) |
|
230 | 230 | cache_size = Integer(1000, config=True, help= |
|
231 | 231 | """ |
|
232 | 232 | Set the size of the output cache. The default is 1000, you can |
|
233 | 233 | change it permanently in your config file. Setting it to 0 completely |
|
234 | 234 | disables the caching system, and the minimum value accepted is 20 (if |
|
235 | 235 | you provide a value less than 20, it is reset to 0 and a warning is |
|
236 | 236 | issued). This limit is defined because otherwise you'll spend more |
|
237 | 237 | time re-flushing a too small cache than working |
|
238 | 238 | """ |
|
239 | 239 | ) |
|
240 | 240 | color_info = CBool(True, config=True, help= |
|
241 | 241 | """ |
|
242 | 242 | Use colors for displaying information about objects. Because this |
|
243 | 243 | information is passed through a pager (like 'less'), and some pagers |
|
244 | 244 | get confused with color codes, this capability can be turned off. |
|
245 | 245 | """ |
|
246 | 246 | ) |
|
247 | 247 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
248 | 248 | default_value=get_default_colors(), config=True, |
|
249 | 249 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
250 | 250 | ) |
|
251 | 251 | colors_force = CBool(False, help= |
|
252 | 252 | """ |
|
253 | 253 | Force use of ANSI color codes, regardless of OS and readline |
|
254 | 254 | availability. |
|
255 | 255 | """ |
|
256 | 256 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
257 | 257 | # without readline on Win32. When the ZMQ formatting system is |
|
258 | 258 | # refactored, this should be removed. |
|
259 | 259 | ) |
|
260 | 260 | debug = CBool(False, config=True) |
|
261 | 261 | deep_reload = CBool(False, config=True, help= |
|
262 | 262 | """ |
|
263 | 263 | Enable deep (recursive) reloading by default. IPython can use the |
|
264 | 264 | deep_reload module which reloads changes in modules recursively (it |
|
265 | 265 | replaces the reload() function, so you don't need to change anything to |
|
266 | 266 | use it). deep_reload() forces a full reload of modules whose code may |
|
267 | 267 | have changed, which the default reload() function does not. When |
|
268 | 268 | deep_reload is off, IPython will use the normal reload(), but |
|
269 | 269 | deep_reload will still be available as dreload(). |
|
270 | 270 | """ |
|
271 | 271 | ) |
|
272 | 272 | display_formatter = Instance(DisplayFormatter) |
|
273 | 273 | displayhook_class = Type(DisplayHook) |
|
274 | 274 | display_pub_class = Type(DisplayPublisher) |
|
275 | 275 | |
|
276 | 276 | exit_now = CBool(False) |
|
277 | 277 | exiter = Instance(ExitAutocall) |
|
278 | 278 | def _exiter_default(self): |
|
279 | 279 | return ExitAutocall(self) |
|
280 | 280 | # Monotonically increasing execution counter |
|
281 | 281 | execution_count = Integer(1) |
|
282 | 282 | filename = Unicode("<ipython console>") |
|
283 | 283 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
284 | 284 | |
|
285 | 285 | # Input splitter, to split entire cells of input into either individual |
|
286 | 286 | # interactive statements or whole blocks. |
|
287 | 287 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
288 | 288 | (), {}) |
|
289 | 289 | logstart = CBool(False, config=True, help= |
|
290 | 290 | """ |
|
291 | 291 | Start logging to the default log file. |
|
292 | 292 | """ |
|
293 | 293 | ) |
|
294 | 294 | logfile = Unicode('', config=True, help= |
|
295 | 295 | """ |
|
296 | 296 | The name of the logfile to use. |
|
297 | 297 | """ |
|
298 | 298 | ) |
|
299 | 299 | logappend = Unicode('', config=True, help= |
|
300 | 300 | """ |
|
301 | 301 | Start logging to the given file in append mode. |
|
302 | 302 | """ |
|
303 | 303 | ) |
|
304 | 304 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
305 | 305 | config=True) |
|
306 | 306 | pdb = CBool(False, config=True, help= |
|
307 | 307 | """ |
|
308 | 308 | Automatically call the pdb debugger after every exception. |
|
309 | 309 | """ |
|
310 | 310 | ) |
|
311 | 311 | multiline_history = CBool(sys.platform != 'win32', config=True, |
|
312 | 312 | help="Save multi-line entries as one entry in readline history" |
|
313 | 313 | ) |
|
314 | 314 | |
|
315 | 315 | prompt_in1 = Unicode('In [\\#]: ', config=True) |
|
316 | 316 | prompt_in2 = Unicode(' .\\D.: ', config=True) |
|
317 | 317 | prompt_out = Unicode('Out[\\#]: ', config=True) |
|
318 | 318 | prompts_pad_left = CBool(True, config=True) |
|
319 | 319 | quiet = CBool(False, config=True) |
|
320 | 320 | |
|
321 | 321 | history_length = Integer(10000, config=True) |
|
322 | 322 | |
|
323 | 323 | # The readline stuff will eventually be moved to the terminal subclass |
|
324 | 324 | # but for now, we can't do that as readline is welded in everywhere. |
|
325 | 325 | readline_use = CBool(True, config=True) |
|
326 | 326 | readline_remove_delims = Unicode('-/~', config=True) |
|
327 | 327 | # don't use \M- bindings by default, because they |
|
328 | 328 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
329 | 329 | readline_parse_and_bind = List([ |
|
330 | 330 | 'tab: complete', |
|
331 | 331 | '"\C-l": clear-screen', |
|
332 | 332 | 'set show-all-if-ambiguous on', |
|
333 | 333 | '"\C-o": tab-insert', |
|
334 | 334 | '"\C-r": reverse-search-history', |
|
335 | 335 | '"\C-s": forward-search-history', |
|
336 | 336 | '"\C-p": history-search-backward', |
|
337 | 337 | '"\C-n": history-search-forward', |
|
338 | 338 | '"\e[A": history-search-backward', |
|
339 | 339 | '"\e[B": history-search-forward', |
|
340 | 340 | '"\C-k": kill-line', |
|
341 | 341 | '"\C-u": unix-line-discard', |
|
342 | 342 | ], allow_none=False, config=True) |
|
343 | 343 | |
|
344 | 344 | # TODO: this part of prompt management should be moved to the frontends. |
|
345 | 345 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
346 | 346 | separate_in = SeparateUnicode('\n', config=True) |
|
347 | 347 | separate_out = SeparateUnicode('', config=True) |
|
348 | 348 | separate_out2 = SeparateUnicode('', config=True) |
|
349 | 349 | wildcards_case_sensitive = CBool(True, config=True) |
|
350 | 350 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
351 | 351 | default_value='Context', config=True) |
|
352 | 352 | |
|
353 | 353 | # Subcomponents of InteractiveShell |
|
354 | 354 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
355 | 355 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
356 | 356 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
357 | 357 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
358 | 358 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
359 | 359 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
360 | 360 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
361 | 361 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
362 | 362 | |
|
363 | 363 | profile_dir = Instance('IPython.core.application.ProfileDir') |
|
364 | 364 | @property |
|
365 | 365 | def profile(self): |
|
366 | 366 | if self.profile_dir is not None: |
|
367 | 367 | name = os.path.basename(self.profile_dir.location) |
|
368 | 368 | return name.replace('profile_','') |
|
369 | 369 | |
|
370 | 370 | |
|
371 | 371 | # Private interface |
|
372 | 372 | _post_execute = Instance(dict) |
|
373 | 373 | |
|
374 | 374 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, |
|
375 | 375 | user_ns=None, user_global_ns=None, |
|
376 | 376 | custom_exceptions=((), None)): |
|
377 | 377 | |
|
378 | 378 | # This is where traits with a config_key argument are updated |
|
379 | 379 | # from the values on config. |
|
380 | 380 | super(InteractiveShell, self).__init__(config=config) |
|
381 | 381 | self.configurables = [self] |
|
382 | 382 | |
|
383 | 383 | # These are relatively independent and stateless |
|
384 | 384 | self.init_ipython_dir(ipython_dir) |
|
385 | 385 | self.init_profile_dir(profile_dir) |
|
386 | 386 | self.init_instance_attrs() |
|
387 | 387 | self.init_environment() |
|
388 | 388 | |
|
389 | 389 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
390 | 390 | self.init_create_namespaces(user_ns, user_global_ns) |
|
391 | 391 | # This has to be done after init_create_namespaces because it uses |
|
392 | 392 | # something in self.user_ns, but before init_sys_modules, which |
|
393 | 393 | # is the first thing to modify sys. |
|
394 | 394 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
395 | 395 | # is created, we are saving the overridden ones here. Not sure if this |
|
396 | 396 | # is what we want to do. |
|
397 | 397 | self.save_sys_module_state() |
|
398 | 398 | self.init_sys_modules() |
|
399 | 399 | |
|
400 | 400 | # While we're trying to have each part of the code directly access what |
|
401 | 401 | # it needs without keeping redundant references to objects, we have too |
|
402 | 402 | # much legacy code that expects ip.db to exist. |
|
403 | 403 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
404 | 404 | |
|
405 | 405 | self.init_history() |
|
406 | 406 | self.init_encoding() |
|
407 | 407 | self.init_prefilter() |
|
408 | 408 | |
|
409 | 409 | Magic.__init__(self, self) |
|
410 | 410 | |
|
411 | 411 | self.init_syntax_highlighting() |
|
412 | 412 | self.init_hooks() |
|
413 | 413 | self.init_pushd_popd_magic() |
|
414 | 414 | # self.init_traceback_handlers use to be here, but we moved it below |
|
415 | 415 | # because it and init_io have to come after init_readline. |
|
416 | 416 | self.init_user_ns() |
|
417 | 417 | self.init_logger() |
|
418 | 418 | self.init_alias() |
|
419 | 419 | self.init_builtins() |
|
420 | 420 | |
|
421 | 421 | # pre_config_initialization |
|
422 | 422 | |
|
423 | 423 | # The next section should contain everything that was in ipmaker. |
|
424 | 424 | self.init_logstart() |
|
425 | 425 | |
|
426 | 426 | # The following was in post_config_initialization |
|
427 | 427 | self.init_inspector() |
|
428 | 428 | # init_readline() must come before init_io(), because init_io uses |
|
429 | 429 | # readline related things. |
|
430 | 430 | self.init_readline() |
|
431 | 431 | # We save this here in case user code replaces raw_input, but it needs |
|
432 | 432 | # to be after init_readline(), because PyPy's readline works by replacing |
|
433 | 433 | # raw_input. |
|
434 | 434 | if py3compat.PY3: |
|
435 | 435 | self.raw_input_original = input |
|
436 | 436 | else: |
|
437 | 437 | self.raw_input_original = raw_input |
|
438 | 438 | # init_completer must come after init_readline, because it needs to |
|
439 | 439 | # know whether readline is present or not system-wide to configure the |
|
440 | 440 | # completers, since the completion machinery can now operate |
|
441 | 441 | # independently of readline (e.g. over the network) |
|
442 | 442 | self.init_completer() |
|
443 | 443 | # TODO: init_io() needs to happen before init_traceback handlers |
|
444 | 444 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
445 | 445 | # This logic in in debugger.Pdb and should eventually be changed. |
|
446 | 446 | self.init_io() |
|
447 | 447 | self.init_traceback_handlers(custom_exceptions) |
|
448 | 448 | self.init_prompts() |
|
449 | 449 | self.init_display_formatter() |
|
450 | 450 | self.init_display_pub() |
|
451 | 451 | self.init_displayhook() |
|
452 | 452 | self.init_reload_doctest() |
|
453 | 453 | self.init_magics() |
|
454 | 454 | self.init_pdb() |
|
455 | 455 | self.init_extension_manager() |
|
456 | 456 | self.init_plugin_manager() |
|
457 | 457 | self.init_payload() |
|
458 | 458 | self.hooks.late_startup_hook() |
|
459 | 459 | atexit.register(self.atexit_operations) |
|
460 | 460 | |
|
461 | 461 | def get_ipython(self): |
|
462 | 462 | """Return the currently running IPython instance.""" |
|
463 | 463 | return self |
|
464 | 464 | |
|
465 | 465 | #------------------------------------------------------------------------- |
|
466 | 466 | # Trait changed handlers |
|
467 | 467 | #------------------------------------------------------------------------- |
|
468 | 468 | |
|
469 | 469 | def _ipython_dir_changed(self, name, new): |
|
470 | 470 | if not os.path.isdir(new): |
|
471 | 471 | os.makedirs(new, mode = 0777) |
|
472 | 472 | |
|
473 | 473 | def set_autoindent(self,value=None): |
|
474 | 474 | """Set the autoindent flag, checking for readline support. |
|
475 | 475 | |
|
476 | 476 | If called with no arguments, it acts as a toggle.""" |
|
477 | 477 | |
|
478 | 478 | if value != 0 and not self.has_readline: |
|
479 | 479 | if os.name == 'posix': |
|
480 | 480 | warn("The auto-indent feature requires the readline library") |
|
481 | 481 | self.autoindent = 0 |
|
482 | 482 | return |
|
483 | 483 | if value is None: |
|
484 | 484 | self.autoindent = not self.autoindent |
|
485 | 485 | else: |
|
486 | 486 | self.autoindent = value |
|
487 | 487 | |
|
488 | 488 | #------------------------------------------------------------------------- |
|
489 | 489 | # init_* methods called by __init__ |
|
490 | 490 | #------------------------------------------------------------------------- |
|
491 | 491 | |
|
492 | 492 | def init_ipython_dir(self, ipython_dir): |
|
493 | 493 | if ipython_dir is not None: |
|
494 | 494 | self.ipython_dir = ipython_dir |
|
495 | 495 | return |
|
496 | 496 | |
|
497 | 497 | self.ipython_dir = get_ipython_dir() |
|
498 | 498 | |
|
499 | 499 | def init_profile_dir(self, profile_dir): |
|
500 | 500 | if profile_dir is not None: |
|
501 | 501 | self.profile_dir = profile_dir |
|
502 | 502 | return |
|
503 | 503 | self.profile_dir =\ |
|
504 | 504 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
505 | 505 | |
|
506 | 506 | def init_instance_attrs(self): |
|
507 | 507 | self.more = False |
|
508 | 508 | |
|
509 | 509 | # command compiler |
|
510 | 510 | self.compile = CachingCompiler() |
|
511 | 511 | |
|
512 | 512 | # Make an empty namespace, which extension writers can rely on both |
|
513 | 513 | # existing and NEVER being used by ipython itself. This gives them a |
|
514 | 514 | # convenient location for storing additional information and state |
|
515 | 515 | # their extensions may require, without fear of collisions with other |
|
516 | 516 | # ipython names that may develop later. |
|
517 | 517 | self.meta = Struct() |
|
518 | 518 | |
|
519 | 519 | # Temporary files used for various purposes. Deleted at exit. |
|
520 | 520 | self.tempfiles = [] |
|
521 | 521 | |
|
522 | 522 | # Keep track of readline usage (later set by init_readline) |
|
523 | 523 | self.has_readline = False |
|
524 | 524 | |
|
525 | 525 | # keep track of where we started running (mainly for crash post-mortem) |
|
526 | 526 | # This is not being used anywhere currently. |
|
527 | 527 | self.starting_dir = os.getcwdu() |
|
528 | 528 | |
|
529 | 529 | # Indentation management |
|
530 | 530 | self.indent_current_nsp = 0 |
|
531 | 531 | |
|
532 | 532 | # Dict to track post-execution functions that have been registered |
|
533 | 533 | self._post_execute = {} |
|
534 | 534 | |
|
535 | 535 | def init_environment(self): |
|
536 | 536 | """Any changes we need to make to the user's environment.""" |
|
537 | 537 | pass |
|
538 | 538 | |
|
539 | 539 | def init_encoding(self): |
|
540 | 540 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
541 | 541 | # under Win32 have it set to None, and we need to have a known valid |
|
542 | 542 | # encoding to use in the raw_input() method |
|
543 | 543 | try: |
|
544 | 544 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
545 | 545 | except AttributeError: |
|
546 | 546 | self.stdin_encoding = 'ascii' |
|
547 | 547 | |
|
548 | 548 | def init_syntax_highlighting(self): |
|
549 | 549 | # Python source parser/formatter for syntax highlighting |
|
550 | 550 | pyformat = PyColorize.Parser().format |
|
551 | 551 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
552 | 552 | |
|
553 | 553 | def init_pushd_popd_magic(self): |
|
554 | 554 | # for pushd/popd management |
|
555 | try: | |
|
556 | 555 |
|
|
557 | except HomeDirError, msg: | |
|
558 | fatal(msg) | |
|
559 | 556 | |
|
560 | 557 | self.dir_stack = [] |
|
561 | 558 | |
|
562 | 559 | def init_logger(self): |
|
563 | 560 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
564 | 561 | logmode='rotate') |
|
565 | 562 | |
|
566 | 563 | def init_logstart(self): |
|
567 | 564 | """Initialize logging in case it was requested at the command line. |
|
568 | 565 | """ |
|
569 | 566 | if self.logappend: |
|
570 | 567 | self.magic_logstart(self.logappend + ' append') |
|
571 | 568 | elif self.logfile: |
|
572 | 569 | self.magic_logstart(self.logfile) |
|
573 | 570 | elif self.logstart: |
|
574 | 571 | self.magic_logstart() |
|
575 | 572 | |
|
576 | 573 | def init_builtins(self): |
|
577 | 574 | self.builtin_trap = BuiltinTrap(shell=self) |
|
578 | 575 | |
|
579 | 576 | def init_inspector(self): |
|
580 | 577 | # Object inspector |
|
581 | 578 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
582 | 579 | PyColorize.ANSICodeColors, |
|
583 | 580 | 'NoColor', |
|
584 | 581 | self.object_info_string_level) |
|
585 | 582 | |
|
586 | 583 | def init_io(self): |
|
587 | 584 | # This will just use sys.stdout and sys.stderr. If you want to |
|
588 | 585 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
589 | 586 | # *before* instantiating this class, because io holds onto |
|
590 | 587 | # references to the underlying streams. |
|
591 | 588 | if sys.platform == 'win32' and self.has_readline: |
|
592 | 589 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
593 | 590 | else: |
|
594 | 591 | io.stdout = io.IOStream(sys.stdout) |
|
595 | 592 | io.stderr = io.IOStream(sys.stderr) |
|
596 | 593 | |
|
597 | 594 | def init_prompts(self): |
|
598 | 595 | # TODO: This is a pass for now because the prompts are managed inside |
|
599 | 596 | # the DisplayHook. Once there is a separate prompt manager, this |
|
600 | 597 | # will initialize that object and all prompt related information. |
|
601 | 598 | pass |
|
602 | 599 | |
|
603 | 600 | def init_display_formatter(self): |
|
604 | 601 | self.display_formatter = DisplayFormatter(config=self.config) |
|
605 | 602 | self.configurables.append(self.display_formatter) |
|
606 | 603 | |
|
607 | 604 | def init_display_pub(self): |
|
608 | 605 | self.display_pub = self.display_pub_class(config=self.config) |
|
609 | 606 | self.configurables.append(self.display_pub) |
|
610 | 607 | |
|
611 | 608 | def init_displayhook(self): |
|
612 | 609 | # Initialize displayhook, set in/out prompts and printing system |
|
613 | 610 | self.displayhook = self.displayhook_class( |
|
614 | 611 | config=self.config, |
|
615 | 612 | shell=self, |
|
616 | 613 | cache_size=self.cache_size, |
|
617 | 614 | input_sep = self.separate_in, |
|
618 | 615 | output_sep = self.separate_out, |
|
619 | 616 | output_sep2 = self.separate_out2, |
|
620 | 617 | ps1 = self.prompt_in1, |
|
621 | 618 | ps2 = self.prompt_in2, |
|
622 | 619 | ps_out = self.prompt_out, |
|
623 | 620 | pad_left = self.prompts_pad_left |
|
624 | 621 | ) |
|
625 | 622 | self.configurables.append(self.displayhook) |
|
626 | 623 | # This is a context manager that installs/revmoes the displayhook at |
|
627 | 624 | # the appropriate time. |
|
628 | 625 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
629 | 626 | |
|
630 | 627 | def init_reload_doctest(self): |
|
631 | 628 | # Do a proper resetting of doctest, including the necessary displayhook |
|
632 | 629 | # monkeypatching |
|
633 | 630 | try: |
|
634 | 631 | doctest_reload() |
|
635 | 632 | except ImportError: |
|
636 | 633 | warn("doctest module does not exist.") |
|
637 | 634 | |
|
638 | 635 | #------------------------------------------------------------------------- |
|
639 | 636 | # Things related to injections into the sys module |
|
640 | 637 | #------------------------------------------------------------------------- |
|
641 | 638 | |
|
642 | 639 | def save_sys_module_state(self): |
|
643 | 640 | """Save the state of hooks in the sys module. |
|
644 | 641 | |
|
645 | 642 | This has to be called after self.user_ns is created. |
|
646 | 643 | """ |
|
647 | 644 | self._orig_sys_module_state = {} |
|
648 | 645 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
649 | 646 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
650 | 647 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
651 | 648 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
652 | 649 | try: |
|
653 | 650 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
654 | 651 | except KeyError: |
|
655 | 652 | pass |
|
656 | 653 | |
|
657 | 654 | def restore_sys_module_state(self): |
|
658 | 655 | """Restore the state of the sys module.""" |
|
659 | 656 | try: |
|
660 | 657 | for k, v in self._orig_sys_module_state.iteritems(): |
|
661 | 658 | setattr(sys, k, v) |
|
662 | 659 | except AttributeError: |
|
663 | 660 | pass |
|
664 | 661 | # Reset what what done in self.init_sys_modules |
|
665 | 662 | try: |
|
666 | 663 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
667 | 664 | except (AttributeError, KeyError): |
|
668 | 665 | pass |
|
669 | 666 | |
|
670 | 667 | #------------------------------------------------------------------------- |
|
671 | 668 | # Things related to hooks |
|
672 | 669 | #------------------------------------------------------------------------- |
|
673 | 670 | |
|
674 | 671 | def init_hooks(self): |
|
675 | 672 | # hooks holds pointers used for user-side customizations |
|
676 | 673 | self.hooks = Struct() |
|
677 | 674 | |
|
678 | 675 | self.strdispatchers = {} |
|
679 | 676 | |
|
680 | 677 | # Set all default hooks, defined in the IPython.hooks module. |
|
681 | 678 | hooks = IPython.core.hooks |
|
682 | 679 | for hook_name in hooks.__all__: |
|
683 | 680 | # default hooks have priority 100, i.e. low; user hooks should have |
|
684 | 681 | # 0-100 priority |
|
685 | 682 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
686 | 683 | |
|
687 | 684 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
688 | 685 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
689 | 686 | |
|
690 | 687 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
691 | 688 | adding your function to one of these hooks, you can modify IPython's |
|
692 | 689 | behavior to call at runtime your own routines.""" |
|
693 | 690 | |
|
694 | 691 | # At some point in the future, this should validate the hook before it |
|
695 | 692 | # accepts it. Probably at least check that the hook takes the number |
|
696 | 693 | # of args it's supposed to. |
|
697 | 694 | |
|
698 | 695 | f = types.MethodType(hook,self) |
|
699 | 696 | |
|
700 | 697 | # check if the hook is for strdispatcher first |
|
701 | 698 | if str_key is not None: |
|
702 | 699 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
703 | 700 | sdp.add_s(str_key, f, priority ) |
|
704 | 701 | self.strdispatchers[name] = sdp |
|
705 | 702 | return |
|
706 | 703 | if re_key is not None: |
|
707 | 704 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
708 | 705 | sdp.add_re(re.compile(re_key), f, priority ) |
|
709 | 706 | self.strdispatchers[name] = sdp |
|
710 | 707 | return |
|
711 | 708 | |
|
712 | 709 | dp = getattr(self.hooks, name, None) |
|
713 | 710 | if name not in IPython.core.hooks.__all__: |
|
714 | 711 | print "Warning! Hook '%s' is not one of %s" % \ |
|
715 | 712 | (name, IPython.core.hooks.__all__ ) |
|
716 | 713 | if not dp: |
|
717 | 714 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
718 | 715 | |
|
719 | 716 | try: |
|
720 | 717 | dp.add(f,priority) |
|
721 | 718 | except AttributeError: |
|
722 | 719 | # it was not commandchain, plain old func - replace |
|
723 | 720 | dp = f |
|
724 | 721 | |
|
725 | 722 | setattr(self.hooks,name, dp) |
|
726 | 723 | |
|
727 | 724 | def register_post_execute(self, func): |
|
728 | 725 | """Register a function for calling after code execution. |
|
729 | 726 | """ |
|
730 | 727 | if not callable(func): |
|
731 | 728 | raise ValueError('argument %s must be callable' % func) |
|
732 | 729 | self._post_execute[func] = True |
|
733 | 730 | |
|
734 | 731 | #------------------------------------------------------------------------- |
|
735 | 732 | # Things related to the "main" module |
|
736 | 733 | #------------------------------------------------------------------------- |
|
737 | 734 | |
|
738 | 735 | def new_main_mod(self,ns=None): |
|
739 | 736 | """Return a new 'main' module object for user code execution. |
|
740 | 737 | """ |
|
741 | 738 | main_mod = self._user_main_module |
|
742 | 739 | init_fakemod_dict(main_mod,ns) |
|
743 | 740 | return main_mod |
|
744 | 741 | |
|
745 | 742 | def cache_main_mod(self,ns,fname): |
|
746 | 743 | """Cache a main module's namespace. |
|
747 | 744 | |
|
748 | 745 | When scripts are executed via %run, we must keep a reference to the |
|
749 | 746 | namespace of their __main__ module (a FakeModule instance) around so |
|
750 | 747 | that Python doesn't clear it, rendering objects defined therein |
|
751 | 748 | useless. |
|
752 | 749 | |
|
753 | 750 | This method keeps said reference in a private dict, keyed by the |
|
754 | 751 | absolute path of the module object (which corresponds to the script |
|
755 | 752 | path). This way, for multiple executions of the same script we only |
|
756 | 753 | keep one copy of the namespace (the last one), thus preventing memory |
|
757 | 754 | leaks from old references while allowing the objects from the last |
|
758 | 755 | execution to be accessible. |
|
759 | 756 | |
|
760 | 757 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
761 | 758 | because of how Python tears down modules (it hard-sets all their |
|
762 | 759 | references to None without regard for reference counts). This method |
|
763 | 760 | must therefore make a *copy* of the given namespace, to allow the |
|
764 | 761 | original module's __dict__ to be cleared and reused. |
|
765 | 762 | |
|
766 | 763 | |
|
767 | 764 | Parameters |
|
768 | 765 | ---------- |
|
769 | 766 | ns : a namespace (a dict, typically) |
|
770 | 767 | |
|
771 | 768 | fname : str |
|
772 | 769 | Filename associated with the namespace. |
|
773 | 770 | |
|
774 | 771 | Examples |
|
775 | 772 | -------- |
|
776 | 773 | |
|
777 | 774 | In [10]: import IPython |
|
778 | 775 | |
|
779 | 776 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
780 | 777 | |
|
781 | 778 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
782 | 779 | Out[12]: True |
|
783 | 780 | """ |
|
784 | 781 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
785 | 782 | |
|
786 | 783 | def clear_main_mod_cache(self): |
|
787 | 784 | """Clear the cache of main modules. |
|
788 | 785 | |
|
789 | 786 | Mainly for use by utilities like %reset. |
|
790 | 787 | |
|
791 | 788 | Examples |
|
792 | 789 | -------- |
|
793 | 790 | |
|
794 | 791 | In [15]: import IPython |
|
795 | 792 | |
|
796 | 793 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
797 | 794 | |
|
798 | 795 | In [17]: len(_ip._main_ns_cache) > 0 |
|
799 | 796 | Out[17]: True |
|
800 | 797 | |
|
801 | 798 | In [18]: _ip.clear_main_mod_cache() |
|
802 | 799 | |
|
803 | 800 | In [19]: len(_ip._main_ns_cache) == 0 |
|
804 | 801 | Out[19]: True |
|
805 | 802 | """ |
|
806 | 803 | self._main_ns_cache.clear() |
|
807 | 804 | |
|
808 | 805 | #------------------------------------------------------------------------- |
|
809 | 806 | # Things related to debugging |
|
810 | 807 | #------------------------------------------------------------------------- |
|
811 | 808 | |
|
812 | 809 | def init_pdb(self): |
|
813 | 810 | # Set calling of pdb on exceptions |
|
814 | 811 | # self.call_pdb is a property |
|
815 | 812 | self.call_pdb = self.pdb |
|
816 | 813 | |
|
817 | 814 | def _get_call_pdb(self): |
|
818 | 815 | return self._call_pdb |
|
819 | 816 | |
|
820 | 817 | def _set_call_pdb(self,val): |
|
821 | 818 | |
|
822 | 819 | if val not in (0,1,False,True): |
|
823 | 820 | raise ValueError,'new call_pdb value must be boolean' |
|
824 | 821 | |
|
825 | 822 | # store value in instance |
|
826 | 823 | self._call_pdb = val |
|
827 | 824 | |
|
828 | 825 | # notify the actual exception handlers |
|
829 | 826 | self.InteractiveTB.call_pdb = val |
|
830 | 827 | |
|
831 | 828 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
832 | 829 | 'Control auto-activation of pdb at exceptions') |
|
833 | 830 | |
|
834 | 831 | def debugger(self,force=False): |
|
835 | 832 | """Call the pydb/pdb debugger. |
|
836 | 833 | |
|
837 | 834 | Keywords: |
|
838 | 835 | |
|
839 | 836 | - force(False): by default, this routine checks the instance call_pdb |
|
840 | 837 | flag and does not actually invoke the debugger if the flag is false. |
|
841 | 838 | The 'force' option forces the debugger to activate even if the flag |
|
842 | 839 | is false. |
|
843 | 840 | """ |
|
844 | 841 | |
|
845 | 842 | if not (force or self.call_pdb): |
|
846 | 843 | return |
|
847 | 844 | |
|
848 | 845 | if not hasattr(sys,'last_traceback'): |
|
849 | 846 | error('No traceback has been produced, nothing to debug.') |
|
850 | 847 | return |
|
851 | 848 | |
|
852 | 849 | # use pydb if available |
|
853 | 850 | if debugger.has_pydb: |
|
854 | 851 | from pydb import pm |
|
855 | 852 | else: |
|
856 | 853 | # fallback to our internal debugger |
|
857 | 854 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
858 | 855 | |
|
859 | 856 | with self.readline_no_record: |
|
860 | 857 | pm() |
|
861 | 858 | |
|
862 | 859 | #------------------------------------------------------------------------- |
|
863 | 860 | # Things related to IPython's various namespaces |
|
864 | 861 | #------------------------------------------------------------------------- |
|
865 | 862 | |
|
866 | 863 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
867 | 864 | # Create the namespace where the user will operate. user_ns is |
|
868 | 865 | # normally the only one used, and it is passed to the exec calls as |
|
869 | 866 | # the locals argument. But we do carry a user_global_ns namespace |
|
870 | 867 | # given as the exec 'globals' argument, This is useful in embedding |
|
871 | 868 | # situations where the ipython shell opens in a context where the |
|
872 | 869 | # distinction between locals and globals is meaningful. For |
|
873 | 870 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
874 | 871 | |
|
875 | 872 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
876 | 873 | # level as a dict instead of a module. This is a manual fix, but I |
|
877 | 874 | # should really track down where the problem is coming from. Alex |
|
878 | 875 | # Schmolck reported this problem first. |
|
879 | 876 | |
|
880 | 877 | # A useful post by Alex Martelli on this topic: |
|
881 | 878 | # Re: inconsistent value from __builtins__ |
|
882 | 879 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
883 | 880 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
884 | 881 | # Gruppen: comp.lang.python |
|
885 | 882 | |
|
886 | 883 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
887 | 884 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
888 | 885 | # > <type 'dict'> |
|
889 | 886 | # > >>> print type(__builtins__) |
|
890 | 887 | # > <type 'module'> |
|
891 | 888 | # > Is this difference in return value intentional? |
|
892 | 889 | |
|
893 | 890 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
894 | 891 | # or a module, and it's been that way for a long time. Whether it's |
|
895 | 892 | # intentional (or sensible), I don't know. In any case, the idea is |
|
896 | 893 | # that if you need to access the built-in namespace directly, you |
|
897 | 894 | # should start with "import __builtin__" (note, no 's') which will |
|
898 | 895 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
899 | 896 | |
|
900 | 897 | # These routines return properly built dicts as needed by the rest of |
|
901 | 898 | # the code, and can also be used by extension writers to generate |
|
902 | 899 | # properly initialized namespaces. |
|
903 | 900 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
904 | 901 | user_global_ns) |
|
905 | 902 | |
|
906 | 903 | # Assign namespaces |
|
907 | 904 | # This is the namespace where all normal user variables live |
|
908 | 905 | self.user_ns = user_ns |
|
909 | 906 | self.user_global_ns = user_global_ns |
|
910 | 907 | |
|
911 | 908 | # An auxiliary namespace that checks what parts of the user_ns were |
|
912 | 909 | # loaded at startup, so we can list later only variables defined in |
|
913 | 910 | # actual interactive use. Since it is always a subset of user_ns, it |
|
914 | 911 | # doesn't need to be separately tracked in the ns_table. |
|
915 | 912 | self.user_ns_hidden = {} |
|
916 | 913 | |
|
917 | 914 | # A namespace to keep track of internal data structures to prevent |
|
918 | 915 | # them from cluttering user-visible stuff. Will be updated later |
|
919 | 916 | self.internal_ns = {} |
|
920 | 917 | |
|
921 | 918 | # Now that FakeModule produces a real module, we've run into a nasty |
|
922 | 919 | # problem: after script execution (via %run), the module where the user |
|
923 | 920 | # code ran is deleted. Now that this object is a true module (needed |
|
924 | 921 | # so docetst and other tools work correctly), the Python module |
|
925 | 922 | # teardown mechanism runs over it, and sets to None every variable |
|
926 | 923 | # present in that module. Top-level references to objects from the |
|
927 | 924 | # script survive, because the user_ns is updated with them. However, |
|
928 | 925 | # calling functions defined in the script that use other things from |
|
929 | 926 | # the script will fail, because the function's closure had references |
|
930 | 927 | # to the original objects, which are now all None. So we must protect |
|
931 | 928 | # these modules from deletion by keeping a cache. |
|
932 | 929 | # |
|
933 | 930 | # To avoid keeping stale modules around (we only need the one from the |
|
934 | 931 | # last run), we use a dict keyed with the full path to the script, so |
|
935 | 932 | # only the last version of the module is held in the cache. Note, |
|
936 | 933 | # however, that we must cache the module *namespace contents* (their |
|
937 | 934 | # __dict__). Because if we try to cache the actual modules, old ones |
|
938 | 935 | # (uncached) could be destroyed while still holding references (such as |
|
939 | 936 | # those held by GUI objects that tend to be long-lived)> |
|
940 | 937 | # |
|
941 | 938 | # The %reset command will flush this cache. See the cache_main_mod() |
|
942 | 939 | # and clear_main_mod_cache() methods for details on use. |
|
943 | 940 | |
|
944 | 941 | # This is the cache used for 'main' namespaces |
|
945 | 942 | self._main_ns_cache = {} |
|
946 | 943 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
947 | 944 | # copying and clearing for reuse on each %run |
|
948 | 945 | self._user_main_module = FakeModule() |
|
949 | 946 | |
|
950 | 947 | # A table holding all the namespaces IPython deals with, so that |
|
951 | 948 | # introspection facilities can search easily. |
|
952 | 949 | self.ns_table = {'user':user_ns, |
|
953 | 950 | 'user_global':user_global_ns, |
|
954 | 951 | 'internal':self.internal_ns, |
|
955 | 952 | 'builtin':builtin_mod.__dict__ |
|
956 | 953 | } |
|
957 | 954 | |
|
958 | 955 | # Similarly, track all namespaces where references can be held and that |
|
959 | 956 | # we can safely clear (so it can NOT include builtin). This one can be |
|
960 | 957 | # a simple list. Note that the main execution namespaces, user_ns and |
|
961 | 958 | # user_global_ns, can NOT be listed here, as clearing them blindly |
|
962 | 959 | # causes errors in object __del__ methods. Instead, the reset() method |
|
963 | 960 | # clears them manually and carefully. |
|
964 | 961 | self.ns_refs_table = [ self.user_ns_hidden, |
|
965 | 962 | self.internal_ns, self._main_ns_cache ] |
|
966 | 963 | |
|
967 | 964 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
968 | 965 | """Return a valid local and global user interactive namespaces. |
|
969 | 966 | |
|
970 | 967 | This builds a dict with the minimal information needed to operate as a |
|
971 | 968 | valid IPython user namespace, which you can pass to the various |
|
972 | 969 | embedding classes in ipython. The default implementation returns the |
|
973 | 970 | same dict for both the locals and the globals to allow functions to |
|
974 | 971 | refer to variables in the namespace. Customized implementations can |
|
975 | 972 | return different dicts. The locals dictionary can actually be anything |
|
976 | 973 | following the basic mapping protocol of a dict, but the globals dict |
|
977 | 974 | must be a true dict, not even a subclass. It is recommended that any |
|
978 | 975 | custom object for the locals namespace synchronize with the globals |
|
979 | 976 | dict somehow. |
|
980 | 977 | |
|
981 | 978 | Raises TypeError if the provided globals namespace is not a true dict. |
|
982 | 979 | |
|
983 | 980 | Parameters |
|
984 | 981 | ---------- |
|
985 | 982 | user_ns : dict-like, optional |
|
986 | 983 | The current user namespace. The items in this namespace should |
|
987 | 984 | be included in the output. If None, an appropriate blank |
|
988 | 985 | namespace should be created. |
|
989 | 986 | user_global_ns : dict, optional |
|
990 | 987 | The current user global namespace. The items in this namespace |
|
991 | 988 | should be included in the output. If None, an appropriate |
|
992 | 989 | blank namespace should be created. |
|
993 | 990 | |
|
994 | 991 | Returns |
|
995 | 992 | ------- |
|
996 | 993 | A pair of dictionary-like object to be used as the local namespace |
|
997 | 994 | of the interpreter and a dict to be used as the global namespace. |
|
998 | 995 | """ |
|
999 | 996 | |
|
1000 | 997 | |
|
1001 | 998 | # We must ensure that __builtin__ (without the final 's') is always |
|
1002 | 999 | # available and pointing to the __builtin__ *module*. For more details: |
|
1003 | 1000 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1004 | 1001 | |
|
1005 | 1002 | if user_ns is None: |
|
1006 | 1003 | # Set __name__ to __main__ to better match the behavior of the |
|
1007 | 1004 | # normal interpreter. |
|
1008 | 1005 | user_ns = {'__name__' :'__main__', |
|
1009 | 1006 | py3compat.builtin_mod_name: builtin_mod, |
|
1010 | 1007 | '__builtins__' : builtin_mod, |
|
1011 | 1008 | } |
|
1012 | 1009 | else: |
|
1013 | 1010 | user_ns.setdefault('__name__','__main__') |
|
1014 | 1011 | user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod) |
|
1015 | 1012 | user_ns.setdefault('__builtins__',builtin_mod) |
|
1016 | 1013 | |
|
1017 | 1014 | if user_global_ns is None: |
|
1018 | 1015 | user_global_ns = user_ns |
|
1019 | 1016 | if type(user_global_ns) is not dict: |
|
1020 | 1017 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
1021 | 1018 | % type(user_global_ns)) |
|
1022 | 1019 | |
|
1023 | 1020 | return user_ns, user_global_ns |
|
1024 | 1021 | |
|
1025 | 1022 | def init_sys_modules(self): |
|
1026 | 1023 | # We need to insert into sys.modules something that looks like a |
|
1027 | 1024 | # module but which accesses the IPython namespace, for shelve and |
|
1028 | 1025 | # pickle to work interactively. Normally they rely on getting |
|
1029 | 1026 | # everything out of __main__, but for embedding purposes each IPython |
|
1030 | 1027 | # instance has its own private namespace, so we can't go shoving |
|
1031 | 1028 | # everything into __main__. |
|
1032 | 1029 | |
|
1033 | 1030 | # note, however, that we should only do this for non-embedded |
|
1034 | 1031 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1035 | 1032 | # namespace. Embedded instances, on the other hand, should not do |
|
1036 | 1033 | # this because they need to manage the user local/global namespaces |
|
1037 | 1034 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1038 | 1035 | # shouldn't overtake the execution environment of the script they're |
|
1039 | 1036 | # embedded in). |
|
1040 | 1037 | |
|
1041 | 1038 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1042 | 1039 | |
|
1043 | 1040 | try: |
|
1044 | 1041 | main_name = self.user_ns['__name__'] |
|
1045 | 1042 | except KeyError: |
|
1046 | 1043 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
1047 | 1044 | else: |
|
1048 | 1045 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
1049 | 1046 | |
|
1050 | 1047 | def init_user_ns(self): |
|
1051 | 1048 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1052 | 1049 | |
|
1053 | 1050 | Certain history lists are also initialized here, as they effectively |
|
1054 | 1051 | act as user namespaces. |
|
1055 | 1052 | |
|
1056 | 1053 | Notes |
|
1057 | 1054 | ----- |
|
1058 | 1055 | All data structures here are only filled in, they are NOT reset by this |
|
1059 | 1056 | method. If they were not empty before, data will simply be added to |
|
1060 | 1057 | therm. |
|
1061 | 1058 | """ |
|
1062 | 1059 | # This function works in two parts: first we put a few things in |
|
1063 | 1060 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1064 | 1061 | # initial variables aren't shown by %who. After the sync, we add the |
|
1065 | 1062 | # rest of what we *do* want the user to see with %who even on a new |
|
1066 | 1063 | # session (probably nothing, so theye really only see their own stuff) |
|
1067 | 1064 | |
|
1068 | 1065 | # The user dict must *always* have a __builtin__ reference to the |
|
1069 | 1066 | # Python standard __builtin__ namespace, which must be imported. |
|
1070 | 1067 | # This is so that certain operations in prompt evaluation can be |
|
1071 | 1068 | # reliably executed with builtins. Note that we can NOT use |
|
1072 | 1069 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1073 | 1070 | # module, and can even mutate at runtime, depending on the context |
|
1074 | 1071 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1075 | 1072 | # always a module object, though it must be explicitly imported. |
|
1076 | 1073 | |
|
1077 | 1074 | # For more details: |
|
1078 | 1075 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1079 | 1076 | ns = dict(__builtin__ = builtin_mod) |
|
1080 | 1077 | |
|
1081 | 1078 | # Put 'help' in the user namespace |
|
1082 | 1079 | try: |
|
1083 | 1080 | from site import _Helper |
|
1084 | 1081 | ns['help'] = _Helper() |
|
1085 | 1082 | except ImportError: |
|
1086 | 1083 | warn('help() not available - check site.py') |
|
1087 | 1084 | |
|
1088 | 1085 | # make global variables for user access to the histories |
|
1089 | 1086 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1090 | 1087 | ns['_oh'] = self.history_manager.output_hist |
|
1091 | 1088 | ns['_dh'] = self.history_manager.dir_hist |
|
1092 | 1089 | |
|
1093 | 1090 | ns['_sh'] = shadowns |
|
1094 | 1091 | |
|
1095 | 1092 | # user aliases to input and output histories. These shouldn't show up |
|
1096 | 1093 | # in %who, as they can have very large reprs. |
|
1097 | 1094 | ns['In'] = self.history_manager.input_hist_parsed |
|
1098 | 1095 | ns['Out'] = self.history_manager.output_hist |
|
1099 | 1096 | |
|
1100 | 1097 | # Store myself as the public api!!! |
|
1101 | 1098 | ns['get_ipython'] = self.get_ipython |
|
1102 | 1099 | |
|
1103 | 1100 | ns['exit'] = self.exiter |
|
1104 | 1101 | ns['quit'] = self.exiter |
|
1105 | 1102 | |
|
1106 | 1103 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1107 | 1104 | # by %who |
|
1108 | 1105 | self.user_ns_hidden.update(ns) |
|
1109 | 1106 | |
|
1110 | 1107 | # Anything put into ns now would show up in %who. Think twice before |
|
1111 | 1108 | # putting anything here, as we really want %who to show the user their |
|
1112 | 1109 | # stuff, not our variables. |
|
1113 | 1110 | |
|
1114 | 1111 | # Finally, update the real user's namespace |
|
1115 | 1112 | self.user_ns.update(ns) |
|
1116 | 1113 | |
|
1117 | 1114 | def reset(self, new_session=True): |
|
1118 | 1115 | """Clear all internal namespaces, and attempt to release references to |
|
1119 | 1116 | user objects. |
|
1120 | 1117 | |
|
1121 | 1118 | If new_session is True, a new history session will be opened. |
|
1122 | 1119 | """ |
|
1123 | 1120 | # Clear histories |
|
1124 | 1121 | self.history_manager.reset(new_session) |
|
1125 | 1122 | # Reset counter used to index all histories |
|
1126 | 1123 | if new_session: |
|
1127 | 1124 | self.execution_count = 1 |
|
1128 | 1125 | |
|
1129 | 1126 | # Flush cached output items |
|
1130 | 1127 | if self.displayhook.do_full_cache: |
|
1131 | 1128 | self.displayhook.flush() |
|
1132 | 1129 | |
|
1133 | 1130 | # Restore the user namespaces to minimal usability |
|
1134 | 1131 | for ns in self.ns_refs_table: |
|
1135 | 1132 | ns.clear() |
|
1136 | 1133 | |
|
1137 | 1134 | # The main execution namespaces must be cleared very carefully, |
|
1138 | 1135 | # skipping the deletion of the builtin-related keys, because doing so |
|
1139 | 1136 | # would cause errors in many object's __del__ methods. |
|
1140 | 1137 | for ns in [self.user_ns, self.user_global_ns]: |
|
1141 | 1138 | drop_keys = set(ns.keys()) |
|
1142 | 1139 | drop_keys.discard('__builtin__') |
|
1143 | 1140 | drop_keys.discard('__builtins__') |
|
1144 | 1141 | for k in drop_keys: |
|
1145 | 1142 | del ns[k] |
|
1146 | 1143 | |
|
1147 | 1144 | # Restore the user namespaces to minimal usability |
|
1148 | 1145 | self.init_user_ns() |
|
1149 | 1146 | |
|
1150 | 1147 | # Restore the default and user aliases |
|
1151 | 1148 | self.alias_manager.clear_aliases() |
|
1152 | 1149 | self.alias_manager.init_aliases() |
|
1153 | 1150 | |
|
1154 | 1151 | # Flush the private list of module references kept for script |
|
1155 | 1152 | # execution protection |
|
1156 | 1153 | self.clear_main_mod_cache() |
|
1157 | 1154 | |
|
1158 | 1155 | # Clear out the namespace from the last %run |
|
1159 | 1156 | self.new_main_mod() |
|
1160 | 1157 | |
|
1161 | 1158 | def del_var(self, varname, by_name=False): |
|
1162 | 1159 | """Delete a variable from the various namespaces, so that, as |
|
1163 | 1160 | far as possible, we're not keeping any hidden references to it. |
|
1164 | 1161 | |
|
1165 | 1162 | Parameters |
|
1166 | 1163 | ---------- |
|
1167 | 1164 | varname : str |
|
1168 | 1165 | The name of the variable to delete. |
|
1169 | 1166 | by_name : bool |
|
1170 | 1167 | If True, delete variables with the given name in each |
|
1171 | 1168 | namespace. If False (default), find the variable in the user |
|
1172 | 1169 | namespace, and delete references to it. |
|
1173 | 1170 | """ |
|
1174 | 1171 | if varname in ('__builtin__', '__builtins__'): |
|
1175 | 1172 | raise ValueError("Refusing to delete %s" % varname) |
|
1176 | 1173 | ns_refs = self.ns_refs_table + [self.user_ns, |
|
1177 | 1174 | self.user_global_ns, self._user_main_module.__dict__] +\ |
|
1178 | 1175 | self._main_ns_cache.values() |
|
1179 | 1176 | |
|
1180 | 1177 | if by_name: # Delete by name |
|
1181 | 1178 | for ns in ns_refs: |
|
1182 | 1179 | try: |
|
1183 | 1180 | del ns[varname] |
|
1184 | 1181 | except KeyError: |
|
1185 | 1182 | pass |
|
1186 | 1183 | else: # Delete by object |
|
1187 | 1184 | try: |
|
1188 | 1185 | obj = self.user_ns[varname] |
|
1189 | 1186 | except KeyError: |
|
1190 | 1187 | raise NameError("name '%s' is not defined" % varname) |
|
1191 | 1188 | # Also check in output history |
|
1192 | 1189 | ns_refs.append(self.history_manager.output_hist) |
|
1193 | 1190 | for ns in ns_refs: |
|
1194 | 1191 | to_delete = [n for n, o in ns.iteritems() if o is obj] |
|
1195 | 1192 | for name in to_delete: |
|
1196 | 1193 | del ns[name] |
|
1197 | 1194 | |
|
1198 | 1195 | # displayhook keeps extra references, but not in a dictionary |
|
1199 | 1196 | for name in ('_', '__', '___'): |
|
1200 | 1197 | if getattr(self.displayhook, name) is obj: |
|
1201 | 1198 | setattr(self.displayhook, name, None) |
|
1202 | 1199 | |
|
1203 | 1200 | def reset_selective(self, regex=None): |
|
1204 | 1201 | """Clear selective variables from internal namespaces based on a |
|
1205 | 1202 | specified regular expression. |
|
1206 | 1203 | |
|
1207 | 1204 | Parameters |
|
1208 | 1205 | ---------- |
|
1209 | 1206 | regex : string or compiled pattern, optional |
|
1210 | 1207 | A regular expression pattern that will be used in searching |
|
1211 | 1208 | variable names in the users namespaces. |
|
1212 | 1209 | """ |
|
1213 | 1210 | if regex is not None: |
|
1214 | 1211 | try: |
|
1215 | 1212 | m = re.compile(regex) |
|
1216 | 1213 | except TypeError: |
|
1217 | 1214 | raise TypeError('regex must be a string or compiled pattern') |
|
1218 | 1215 | # Search for keys in each namespace that match the given regex |
|
1219 | 1216 | # If a match is found, delete the key/value pair. |
|
1220 | 1217 | for ns in self.ns_refs_table: |
|
1221 | 1218 | for var in ns: |
|
1222 | 1219 | if m.search(var): |
|
1223 | 1220 | del ns[var] |
|
1224 | 1221 | |
|
1225 | 1222 | def push(self, variables, interactive=True): |
|
1226 | 1223 | """Inject a group of variables into the IPython user namespace. |
|
1227 | 1224 | |
|
1228 | 1225 | Parameters |
|
1229 | 1226 | ---------- |
|
1230 | 1227 | variables : dict, str or list/tuple of str |
|
1231 | 1228 | The variables to inject into the user's namespace. If a dict, a |
|
1232 | 1229 | simple update is done. If a str, the string is assumed to have |
|
1233 | 1230 | variable names separated by spaces. A list/tuple of str can also |
|
1234 | 1231 | be used to give the variable names. If just the variable names are |
|
1235 | 1232 | give (list/tuple/str) then the variable values looked up in the |
|
1236 | 1233 | callers frame. |
|
1237 | 1234 | interactive : bool |
|
1238 | 1235 | If True (default), the variables will be listed with the ``who`` |
|
1239 | 1236 | magic. |
|
1240 | 1237 | """ |
|
1241 | 1238 | vdict = None |
|
1242 | 1239 | |
|
1243 | 1240 | # We need a dict of name/value pairs to do namespace updates. |
|
1244 | 1241 | if isinstance(variables, dict): |
|
1245 | 1242 | vdict = variables |
|
1246 | 1243 | elif isinstance(variables, (basestring, list, tuple)): |
|
1247 | 1244 | if isinstance(variables, basestring): |
|
1248 | 1245 | vlist = variables.split() |
|
1249 | 1246 | else: |
|
1250 | 1247 | vlist = variables |
|
1251 | 1248 | vdict = {} |
|
1252 | 1249 | cf = sys._getframe(1) |
|
1253 | 1250 | for name in vlist: |
|
1254 | 1251 | try: |
|
1255 | 1252 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1256 | 1253 | except: |
|
1257 | 1254 | print ('Could not get variable %s from %s' % |
|
1258 | 1255 | (name,cf.f_code.co_name)) |
|
1259 | 1256 | else: |
|
1260 | 1257 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1261 | 1258 | |
|
1262 | 1259 | # Propagate variables to user namespace |
|
1263 | 1260 | self.user_ns.update(vdict) |
|
1264 | 1261 | |
|
1265 | 1262 | # And configure interactive visibility |
|
1266 | 1263 | config_ns = self.user_ns_hidden |
|
1267 | 1264 | if interactive: |
|
1268 | 1265 | for name, val in vdict.iteritems(): |
|
1269 | 1266 | config_ns.pop(name, None) |
|
1270 | 1267 | else: |
|
1271 | 1268 | for name,val in vdict.iteritems(): |
|
1272 | 1269 | config_ns[name] = val |
|
1273 | 1270 | |
|
1274 | 1271 | def drop_by_id(self, variables): |
|
1275 | 1272 | """Remove a dict of variables from the user namespace, if they are the |
|
1276 | 1273 | same as the values in the dictionary. |
|
1277 | 1274 | |
|
1278 | 1275 | This is intended for use by extensions: variables that they've added can |
|
1279 | 1276 | be taken back out if they are unloaded, without removing any that the |
|
1280 | 1277 | user has overwritten. |
|
1281 | 1278 | |
|
1282 | 1279 | Parameters |
|
1283 | 1280 | ---------- |
|
1284 | 1281 | variables : dict |
|
1285 | 1282 | A dictionary mapping object names (as strings) to the objects. |
|
1286 | 1283 | """ |
|
1287 | 1284 | for name, obj in variables.iteritems(): |
|
1288 | 1285 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1289 | 1286 | del self.user_ns[name] |
|
1290 | 1287 | self.user_ns_hidden.pop(name, None) |
|
1291 | 1288 | |
|
1292 | 1289 | #------------------------------------------------------------------------- |
|
1293 | 1290 | # Things related to object introspection |
|
1294 | 1291 | #------------------------------------------------------------------------- |
|
1295 | 1292 | |
|
1296 | 1293 | def _ofind(self, oname, namespaces=None): |
|
1297 | 1294 | """Find an object in the available namespaces. |
|
1298 | 1295 | |
|
1299 | 1296 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1300 | 1297 | |
|
1301 | 1298 | Has special code to detect magic functions. |
|
1302 | 1299 | """ |
|
1303 | 1300 | oname = oname.strip() |
|
1304 | 1301 | #print '1- oname: <%r>' % oname # dbg |
|
1305 | 1302 | if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True): |
|
1306 | 1303 | return dict(found=False) |
|
1307 | 1304 | |
|
1308 | 1305 | alias_ns = None |
|
1309 | 1306 | if namespaces is None: |
|
1310 | 1307 | # Namespaces to search in: |
|
1311 | 1308 | # Put them in a list. The order is important so that we |
|
1312 | 1309 | # find things in the same order that Python finds them. |
|
1313 | 1310 | namespaces = [ ('Interactive', self.user_ns), |
|
1314 | 1311 | ('IPython internal', self.internal_ns), |
|
1315 | 1312 | ('Python builtin', builtin_mod.__dict__), |
|
1316 | 1313 | ('Alias', self.alias_manager.alias_table), |
|
1317 | 1314 | ] |
|
1318 | 1315 | alias_ns = self.alias_manager.alias_table |
|
1319 | 1316 | |
|
1320 | 1317 | # initialize results to 'null' |
|
1321 | 1318 | found = False; obj = None; ospace = None; ds = None; |
|
1322 | 1319 | ismagic = False; isalias = False; parent = None |
|
1323 | 1320 | |
|
1324 | 1321 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1325 | 1322 | # function but should only be treated as one if print_function was |
|
1326 | 1323 | # loaded with a future import. In this case, just bail. |
|
1327 | 1324 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1328 | 1325 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1329 | 1326 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1330 | 1327 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1331 | 1328 | |
|
1332 | 1329 | # Look for the given name by splitting it in parts. If the head is |
|
1333 | 1330 | # found, then we look for all the remaining parts as members, and only |
|
1334 | 1331 | # declare success if we can find them all. |
|
1335 | 1332 | oname_parts = oname.split('.') |
|
1336 | 1333 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1337 | 1334 | for nsname,ns in namespaces: |
|
1338 | 1335 | try: |
|
1339 | 1336 | obj = ns[oname_head] |
|
1340 | 1337 | except KeyError: |
|
1341 | 1338 | continue |
|
1342 | 1339 | else: |
|
1343 | 1340 | #print 'oname_rest:', oname_rest # dbg |
|
1344 | 1341 | for part in oname_rest: |
|
1345 | 1342 | try: |
|
1346 | 1343 | parent = obj |
|
1347 | 1344 | obj = getattr(obj,part) |
|
1348 | 1345 | except: |
|
1349 | 1346 | # Blanket except b/c some badly implemented objects |
|
1350 | 1347 | # allow __getattr__ to raise exceptions other than |
|
1351 | 1348 | # AttributeError, which then crashes IPython. |
|
1352 | 1349 | break |
|
1353 | 1350 | else: |
|
1354 | 1351 | # If we finish the for loop (no break), we got all members |
|
1355 | 1352 | found = True |
|
1356 | 1353 | ospace = nsname |
|
1357 | 1354 | if ns == alias_ns: |
|
1358 | 1355 | isalias = True |
|
1359 | 1356 | break # namespace loop |
|
1360 | 1357 | |
|
1361 | 1358 | # Try to see if it's magic |
|
1362 | 1359 | if not found: |
|
1363 | 1360 | if oname.startswith(ESC_MAGIC): |
|
1364 | 1361 | oname = oname[1:] |
|
1365 | 1362 | obj = getattr(self,'magic_'+oname,None) |
|
1366 | 1363 | if obj is not None: |
|
1367 | 1364 | found = True |
|
1368 | 1365 | ospace = 'IPython internal' |
|
1369 | 1366 | ismagic = True |
|
1370 | 1367 | |
|
1371 | 1368 | # Last try: special-case some literals like '', [], {}, etc: |
|
1372 | 1369 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1373 | 1370 | obj = eval(oname_head) |
|
1374 | 1371 | found = True |
|
1375 | 1372 | ospace = 'Interactive' |
|
1376 | 1373 | |
|
1377 | 1374 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1378 | 1375 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1379 | 1376 | |
|
1380 | 1377 | def _ofind_property(self, oname, info): |
|
1381 | 1378 | """Second part of object finding, to look for property details.""" |
|
1382 | 1379 | if info.found: |
|
1383 | 1380 | # Get the docstring of the class property if it exists. |
|
1384 | 1381 | path = oname.split('.') |
|
1385 | 1382 | root = '.'.join(path[:-1]) |
|
1386 | 1383 | if info.parent is not None: |
|
1387 | 1384 | try: |
|
1388 | 1385 | target = getattr(info.parent, '__class__') |
|
1389 | 1386 | # The object belongs to a class instance. |
|
1390 | 1387 | try: |
|
1391 | 1388 | target = getattr(target, path[-1]) |
|
1392 | 1389 | # The class defines the object. |
|
1393 | 1390 | if isinstance(target, property): |
|
1394 | 1391 | oname = root + '.__class__.' + path[-1] |
|
1395 | 1392 | info = Struct(self._ofind(oname)) |
|
1396 | 1393 | except AttributeError: pass |
|
1397 | 1394 | except AttributeError: pass |
|
1398 | 1395 | |
|
1399 | 1396 | # We return either the new info or the unmodified input if the object |
|
1400 | 1397 | # hadn't been found |
|
1401 | 1398 | return info |
|
1402 | 1399 | |
|
1403 | 1400 | def _object_find(self, oname, namespaces=None): |
|
1404 | 1401 | """Find an object and return a struct with info about it.""" |
|
1405 | 1402 | inf = Struct(self._ofind(oname, namespaces)) |
|
1406 | 1403 | return Struct(self._ofind_property(oname, inf)) |
|
1407 | 1404 | |
|
1408 | 1405 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1409 | 1406 | """Generic interface to the inspector system. |
|
1410 | 1407 | |
|
1411 | 1408 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1412 | 1409 | info = self._object_find(oname) |
|
1413 | 1410 | if info.found: |
|
1414 | 1411 | pmethod = getattr(self.inspector, meth) |
|
1415 | 1412 | formatter = format_screen if info.ismagic else None |
|
1416 | 1413 | if meth == 'pdoc': |
|
1417 | 1414 | pmethod(info.obj, oname, formatter) |
|
1418 | 1415 | elif meth == 'pinfo': |
|
1419 | 1416 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1420 | 1417 | else: |
|
1421 | 1418 | pmethod(info.obj, oname) |
|
1422 | 1419 | else: |
|
1423 | 1420 | print 'Object `%s` not found.' % oname |
|
1424 | 1421 | return 'not found' # so callers can take other action |
|
1425 | 1422 | |
|
1426 | 1423 | def object_inspect(self, oname): |
|
1427 | 1424 | with self.builtin_trap: |
|
1428 | 1425 | info = self._object_find(oname) |
|
1429 | 1426 | if info.found: |
|
1430 | 1427 | return self.inspector.info(info.obj, oname, info=info) |
|
1431 | 1428 | else: |
|
1432 | 1429 | return oinspect.object_info(name=oname, found=False) |
|
1433 | 1430 | |
|
1434 | 1431 | #------------------------------------------------------------------------- |
|
1435 | 1432 | # Things related to history management |
|
1436 | 1433 | #------------------------------------------------------------------------- |
|
1437 | 1434 | |
|
1438 | 1435 | def init_history(self): |
|
1439 | 1436 | """Sets up the command history, and starts regular autosaves.""" |
|
1440 | 1437 | self.history_manager = HistoryManager(shell=self, config=self.config) |
|
1441 | 1438 | self.configurables.append(self.history_manager) |
|
1442 | 1439 | |
|
1443 | 1440 | #------------------------------------------------------------------------- |
|
1444 | 1441 | # Things related to exception handling and tracebacks (not debugging) |
|
1445 | 1442 | #------------------------------------------------------------------------- |
|
1446 | 1443 | |
|
1447 | 1444 | def init_traceback_handlers(self, custom_exceptions): |
|
1448 | 1445 | # Syntax error handler. |
|
1449 | 1446 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1450 | 1447 | |
|
1451 | 1448 | # The interactive one is initialized with an offset, meaning we always |
|
1452 | 1449 | # want to remove the topmost item in the traceback, which is our own |
|
1453 | 1450 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1454 | 1451 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1455 | 1452 | color_scheme='NoColor', |
|
1456 | 1453 | tb_offset = 1, |
|
1457 | 1454 | check_cache=self.compile.check_cache) |
|
1458 | 1455 | |
|
1459 | 1456 | # The instance will store a pointer to the system-wide exception hook, |
|
1460 | 1457 | # so that runtime code (such as magics) can access it. This is because |
|
1461 | 1458 | # during the read-eval loop, it may get temporarily overwritten. |
|
1462 | 1459 | self.sys_excepthook = sys.excepthook |
|
1463 | 1460 | |
|
1464 | 1461 | # and add any custom exception handlers the user may have specified |
|
1465 | 1462 | self.set_custom_exc(*custom_exceptions) |
|
1466 | 1463 | |
|
1467 | 1464 | # Set the exception mode |
|
1468 | 1465 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1469 | 1466 | |
|
1470 | 1467 | def set_custom_exc(self, exc_tuple, handler): |
|
1471 | 1468 | """set_custom_exc(exc_tuple,handler) |
|
1472 | 1469 | |
|
1473 | 1470 | Set a custom exception handler, which will be called if any of the |
|
1474 | 1471 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1475 | 1472 | run_code() method). |
|
1476 | 1473 | |
|
1477 | 1474 | Parameters |
|
1478 | 1475 | ---------- |
|
1479 | 1476 | |
|
1480 | 1477 | exc_tuple : tuple of exception classes |
|
1481 | 1478 | A *tuple* of exception classes, for which to call the defined |
|
1482 | 1479 | handler. It is very important that you use a tuple, and NOT A |
|
1483 | 1480 | LIST here, because of the way Python's except statement works. If |
|
1484 | 1481 | you only want to trap a single exception, use a singleton tuple:: |
|
1485 | 1482 | |
|
1486 | 1483 | exc_tuple == (MyCustomException,) |
|
1487 | 1484 | |
|
1488 | 1485 | handler : callable |
|
1489 | 1486 | handler must have the following signature:: |
|
1490 | 1487 | |
|
1491 | 1488 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1492 | 1489 | ... |
|
1493 | 1490 | return structured_traceback |
|
1494 | 1491 | |
|
1495 | 1492 | Your handler must return a structured traceback (a list of strings), |
|
1496 | 1493 | or None. |
|
1497 | 1494 | |
|
1498 | 1495 | This will be made into an instance method (via types.MethodType) |
|
1499 | 1496 | of IPython itself, and it will be called if any of the exceptions |
|
1500 | 1497 | listed in the exc_tuple are caught. If the handler is None, an |
|
1501 | 1498 | internal basic one is used, which just prints basic info. |
|
1502 | 1499 | |
|
1503 | 1500 | To protect IPython from crashes, if your handler ever raises an |
|
1504 | 1501 | exception or returns an invalid result, it will be immediately |
|
1505 | 1502 | disabled. |
|
1506 | 1503 | |
|
1507 | 1504 | WARNING: by putting in your own exception handler into IPython's main |
|
1508 | 1505 | execution loop, you run a very good chance of nasty crashes. This |
|
1509 | 1506 | facility should only be used if you really know what you are doing.""" |
|
1510 | 1507 | |
|
1511 | 1508 | assert type(exc_tuple)==type(()) , \ |
|
1512 | 1509 | "The custom exceptions must be given AS A TUPLE." |
|
1513 | 1510 | |
|
1514 | 1511 | def dummy_handler(self,etype,value,tb,tb_offset=None): |
|
1515 | 1512 | print '*** Simple custom exception handler ***' |
|
1516 | 1513 | print 'Exception type :',etype |
|
1517 | 1514 | print 'Exception value:',value |
|
1518 | 1515 | print 'Traceback :',tb |
|
1519 | 1516 | #print 'Source code :','\n'.join(self.buffer) |
|
1520 | 1517 | |
|
1521 | 1518 | def validate_stb(stb): |
|
1522 | 1519 | """validate structured traceback return type |
|
1523 | 1520 | |
|
1524 | 1521 | return type of CustomTB *should* be a list of strings, but allow |
|
1525 | 1522 | single strings or None, which are harmless. |
|
1526 | 1523 | |
|
1527 | 1524 | This function will *always* return a list of strings, |
|
1528 | 1525 | and will raise a TypeError if stb is inappropriate. |
|
1529 | 1526 | """ |
|
1530 | 1527 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1531 | 1528 | if stb is None: |
|
1532 | 1529 | return [] |
|
1533 | 1530 | elif isinstance(stb, basestring): |
|
1534 | 1531 | return [stb] |
|
1535 | 1532 | elif not isinstance(stb, list): |
|
1536 | 1533 | raise TypeError(msg) |
|
1537 | 1534 | # it's a list |
|
1538 | 1535 | for line in stb: |
|
1539 | 1536 | # check every element |
|
1540 | 1537 | if not isinstance(line, basestring): |
|
1541 | 1538 | raise TypeError(msg) |
|
1542 | 1539 | return stb |
|
1543 | 1540 | |
|
1544 | 1541 | if handler is None: |
|
1545 | 1542 | wrapped = dummy_handler |
|
1546 | 1543 | else: |
|
1547 | 1544 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1548 | 1545 | """wrap CustomTB handler, to protect IPython from user code |
|
1549 | 1546 | |
|
1550 | 1547 | This makes it harder (but not impossible) for custom exception |
|
1551 | 1548 | handlers to crash IPython. |
|
1552 | 1549 | """ |
|
1553 | 1550 | try: |
|
1554 | 1551 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1555 | 1552 | return validate_stb(stb) |
|
1556 | 1553 | except: |
|
1557 | 1554 | # clear custom handler immediately |
|
1558 | 1555 | self.set_custom_exc((), None) |
|
1559 | 1556 | print >> io.stderr, "Custom TB Handler failed, unregistering" |
|
1560 | 1557 | # show the exception in handler first |
|
1561 | 1558 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1562 | 1559 | print >> io.stdout, self.InteractiveTB.stb2text(stb) |
|
1563 | 1560 | print >> io.stdout, "The original exception:" |
|
1564 | 1561 | stb = self.InteractiveTB.structured_traceback( |
|
1565 | 1562 | (etype,value,tb), tb_offset=tb_offset |
|
1566 | 1563 | ) |
|
1567 | 1564 | return stb |
|
1568 | 1565 | |
|
1569 | 1566 | self.CustomTB = types.MethodType(wrapped,self) |
|
1570 | 1567 | self.custom_exceptions = exc_tuple |
|
1571 | 1568 | |
|
1572 | 1569 | def excepthook(self, etype, value, tb): |
|
1573 | 1570 | """One more defense for GUI apps that call sys.excepthook. |
|
1574 | 1571 | |
|
1575 | 1572 | GUI frameworks like wxPython trap exceptions and call |
|
1576 | 1573 | sys.excepthook themselves. I guess this is a feature that |
|
1577 | 1574 | enables them to keep running after exceptions that would |
|
1578 | 1575 | otherwise kill their mainloop. This is a bother for IPython |
|
1579 | 1576 | which excepts to catch all of the program exceptions with a try: |
|
1580 | 1577 | except: statement. |
|
1581 | 1578 | |
|
1582 | 1579 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1583 | 1580 | any app directly invokes sys.excepthook, it will look to the user like |
|
1584 | 1581 | IPython crashed. In order to work around this, we can disable the |
|
1585 | 1582 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1586 | 1583 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1587 | 1584 | call sys.excepthook will generate a regular-looking exception from |
|
1588 | 1585 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1589 | 1586 | crashes. |
|
1590 | 1587 | |
|
1591 | 1588 | This hook should be used sparingly, only in places which are not likely |
|
1592 | 1589 | to be true IPython errors. |
|
1593 | 1590 | """ |
|
1594 | 1591 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1595 | 1592 | |
|
1596 | 1593 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1597 | 1594 | exception_only=False): |
|
1598 | 1595 | """Display the exception that just occurred. |
|
1599 | 1596 | |
|
1600 | 1597 | If nothing is known about the exception, this is the method which |
|
1601 | 1598 | should be used throughout the code for presenting user tracebacks, |
|
1602 | 1599 | rather than directly invoking the InteractiveTB object. |
|
1603 | 1600 | |
|
1604 | 1601 | A specific showsyntaxerror() also exists, but this method can take |
|
1605 | 1602 | care of calling it if needed, so unless you are explicitly catching a |
|
1606 | 1603 | SyntaxError exception, don't try to analyze the stack manually and |
|
1607 | 1604 | simply call this method.""" |
|
1608 | 1605 | |
|
1609 | 1606 | try: |
|
1610 | 1607 | if exc_tuple is None: |
|
1611 | 1608 | etype, value, tb = sys.exc_info() |
|
1612 | 1609 | else: |
|
1613 | 1610 | etype, value, tb = exc_tuple |
|
1614 | 1611 | |
|
1615 | 1612 | if etype is None: |
|
1616 | 1613 | if hasattr(sys, 'last_type'): |
|
1617 | 1614 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1618 | 1615 | sys.last_traceback |
|
1619 | 1616 | else: |
|
1620 | 1617 | self.write_err('No traceback available to show.\n') |
|
1621 | 1618 | return |
|
1622 | 1619 | |
|
1623 | 1620 | if etype is SyntaxError: |
|
1624 | 1621 | # Though this won't be called by syntax errors in the input |
|
1625 | 1622 | # line, there may be SyntaxError cases with imported code. |
|
1626 | 1623 | self.showsyntaxerror(filename) |
|
1627 | 1624 | elif etype is UsageError: |
|
1628 | 1625 | self.write_err("UsageError: %s" % value) |
|
1629 | 1626 | else: |
|
1630 | 1627 | # WARNING: these variables are somewhat deprecated and not |
|
1631 | 1628 | # necessarily safe to use in a threaded environment, but tools |
|
1632 | 1629 | # like pdb depend on their existence, so let's set them. If we |
|
1633 | 1630 | # find problems in the field, we'll need to revisit their use. |
|
1634 | 1631 | sys.last_type = etype |
|
1635 | 1632 | sys.last_value = value |
|
1636 | 1633 | sys.last_traceback = tb |
|
1637 | 1634 | if etype in self.custom_exceptions: |
|
1638 | 1635 | stb = self.CustomTB(etype, value, tb, tb_offset) |
|
1639 | 1636 | else: |
|
1640 | 1637 | if exception_only: |
|
1641 | 1638 | stb = ['An exception has occurred, use %tb to see ' |
|
1642 | 1639 | 'the full traceback.\n'] |
|
1643 | 1640 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1644 | 1641 | value)) |
|
1645 | 1642 | else: |
|
1646 | 1643 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1647 | 1644 | value, tb, tb_offset=tb_offset) |
|
1648 | 1645 | |
|
1649 | 1646 | self._showtraceback(etype, value, stb) |
|
1650 | 1647 | if self.call_pdb: |
|
1651 | 1648 | # drop into debugger |
|
1652 | 1649 | self.debugger(force=True) |
|
1653 | 1650 | return |
|
1654 | 1651 | |
|
1655 | 1652 | # Actually show the traceback |
|
1656 | 1653 | self._showtraceback(etype, value, stb) |
|
1657 | 1654 | |
|
1658 | 1655 | except KeyboardInterrupt: |
|
1659 | 1656 | self.write_err("\nKeyboardInterrupt\n") |
|
1660 | 1657 | |
|
1661 | 1658 | def _showtraceback(self, etype, evalue, stb): |
|
1662 | 1659 | """Actually show a traceback. |
|
1663 | 1660 | |
|
1664 | 1661 | Subclasses may override this method to put the traceback on a different |
|
1665 | 1662 | place, like a side channel. |
|
1666 | 1663 | """ |
|
1667 | 1664 | print >> io.stdout, self.InteractiveTB.stb2text(stb) |
|
1668 | 1665 | |
|
1669 | 1666 | def showsyntaxerror(self, filename=None): |
|
1670 | 1667 | """Display the syntax error that just occurred. |
|
1671 | 1668 | |
|
1672 | 1669 | This doesn't display a stack trace because there isn't one. |
|
1673 | 1670 | |
|
1674 | 1671 | If a filename is given, it is stuffed in the exception instead |
|
1675 | 1672 | of what was there before (because Python's parser always uses |
|
1676 | 1673 | "<string>" when reading from a string). |
|
1677 | 1674 | """ |
|
1678 | 1675 | etype, value, last_traceback = sys.exc_info() |
|
1679 | 1676 | |
|
1680 | 1677 | # See note about these variables in showtraceback() above |
|
1681 | 1678 | sys.last_type = etype |
|
1682 | 1679 | sys.last_value = value |
|
1683 | 1680 | sys.last_traceback = last_traceback |
|
1684 | 1681 | |
|
1685 | 1682 | if filename and etype is SyntaxError: |
|
1686 | 1683 | # Work hard to stuff the correct filename in the exception |
|
1687 | 1684 | try: |
|
1688 | 1685 | msg, (dummy_filename, lineno, offset, line) = value |
|
1689 | 1686 | except: |
|
1690 | 1687 | # Not the format we expect; leave it alone |
|
1691 | 1688 | pass |
|
1692 | 1689 | else: |
|
1693 | 1690 | # Stuff in the right filename |
|
1694 | 1691 | try: |
|
1695 | 1692 | # Assume SyntaxError is a class exception |
|
1696 | 1693 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1697 | 1694 | except: |
|
1698 | 1695 | # If that failed, assume SyntaxError is a string |
|
1699 | 1696 | value = msg, (filename, lineno, offset, line) |
|
1700 | 1697 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1701 | 1698 | self._showtraceback(etype, value, stb) |
|
1702 | 1699 | |
|
1703 | 1700 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1704 | 1701 | # the %paste magic. |
|
1705 | 1702 | def showindentationerror(self): |
|
1706 | 1703 | """Called by run_cell when there's an IndentationError in code entered |
|
1707 | 1704 | at the prompt. |
|
1708 | 1705 | |
|
1709 | 1706 | This is overridden in TerminalInteractiveShell to show a message about |
|
1710 | 1707 | the %paste magic.""" |
|
1711 | 1708 | self.showsyntaxerror() |
|
1712 | 1709 | |
|
1713 | 1710 | #------------------------------------------------------------------------- |
|
1714 | 1711 | # Things related to readline |
|
1715 | 1712 | #------------------------------------------------------------------------- |
|
1716 | 1713 | |
|
1717 | 1714 | def init_readline(self): |
|
1718 | 1715 | """Command history completion/saving/reloading.""" |
|
1719 | 1716 | |
|
1720 | 1717 | if self.readline_use: |
|
1721 | 1718 | import IPython.utils.rlineimpl as readline |
|
1722 | 1719 | |
|
1723 | 1720 | self.rl_next_input = None |
|
1724 | 1721 | self.rl_do_indent = False |
|
1725 | 1722 | |
|
1726 | 1723 | if not self.readline_use or not readline.have_readline: |
|
1727 | 1724 | self.has_readline = False |
|
1728 | 1725 | self.readline = None |
|
1729 | 1726 | # Set a number of methods that depend on readline to be no-op |
|
1730 | 1727 | self.readline_no_record = no_op_context |
|
1731 | 1728 | self.set_readline_completer = no_op |
|
1732 | 1729 | self.set_custom_completer = no_op |
|
1733 | 1730 | self.set_completer_frame = no_op |
|
1734 | 1731 | if self.readline_use: |
|
1735 | 1732 | warn('Readline services not available or not loaded.') |
|
1736 | 1733 | else: |
|
1737 | 1734 | self.has_readline = True |
|
1738 | 1735 | self.readline = readline |
|
1739 | 1736 | sys.modules['readline'] = readline |
|
1740 | 1737 | |
|
1741 | 1738 | # Platform-specific configuration |
|
1742 | 1739 | if os.name == 'nt': |
|
1743 | 1740 | # FIXME - check with Frederick to see if we can harmonize |
|
1744 | 1741 | # naming conventions with pyreadline to avoid this |
|
1745 | 1742 | # platform-dependent check |
|
1746 | 1743 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1747 | 1744 | else: |
|
1748 | 1745 | self.readline_startup_hook = readline.set_startup_hook |
|
1749 | 1746 | |
|
1750 | 1747 | # Load user's initrc file (readline config) |
|
1751 | 1748 | # Or if libedit is used, load editrc. |
|
1752 | 1749 | inputrc_name = os.environ.get('INPUTRC') |
|
1753 | 1750 | if inputrc_name is None: |
|
1754 | home_dir = get_home_dir() | |
|
1755 | if home_dir is not None: | |
|
1756 | 1751 |
|
|
1757 | 1752 |
|
|
1758 | 1753 |
|
|
1759 |
|
|
|
1754 | inputrc_name = os.path.join(self.home_dir, inputrc_name) | |
|
1760 | 1755 | if os.path.isfile(inputrc_name): |
|
1761 | 1756 | try: |
|
1762 | 1757 | readline.read_init_file(inputrc_name) |
|
1763 | 1758 | except: |
|
1764 | 1759 | warn('Problems reading readline initialization file <%s>' |
|
1765 | 1760 | % inputrc_name) |
|
1766 | 1761 | |
|
1767 | 1762 | # Configure readline according to user's prefs |
|
1768 | 1763 | # This is only done if GNU readline is being used. If libedit |
|
1769 | 1764 | # is being used (as on Leopard) the readline config is |
|
1770 | 1765 | # not run as the syntax for libedit is different. |
|
1771 | 1766 | if not readline.uses_libedit: |
|
1772 | 1767 | for rlcommand in self.readline_parse_and_bind: |
|
1773 | 1768 | #print "loading rl:",rlcommand # dbg |
|
1774 | 1769 | readline.parse_and_bind(rlcommand) |
|
1775 | 1770 | |
|
1776 | 1771 | # Remove some chars from the delimiters list. If we encounter |
|
1777 | 1772 | # unicode chars, discard them. |
|
1778 | 1773 | delims = readline.get_completer_delims() |
|
1779 | 1774 | if not py3compat.PY3: |
|
1780 | 1775 | delims = delims.encode("ascii", "ignore") |
|
1781 | 1776 | for d in self.readline_remove_delims: |
|
1782 | 1777 | delims = delims.replace(d, "") |
|
1783 | 1778 | delims = delims.replace(ESC_MAGIC, '') |
|
1784 | 1779 | readline.set_completer_delims(delims) |
|
1785 | 1780 | # otherwise we end up with a monster history after a while: |
|
1786 | 1781 | readline.set_history_length(self.history_length) |
|
1787 | 1782 | |
|
1788 | 1783 | self.refill_readline_hist() |
|
1789 | 1784 | self.readline_no_record = ReadlineNoRecord(self) |
|
1790 | 1785 | |
|
1791 | 1786 | # Configure auto-indent for all platforms |
|
1792 | 1787 | self.set_autoindent(self.autoindent) |
|
1793 | 1788 | |
|
1794 | 1789 | def refill_readline_hist(self): |
|
1795 | 1790 | # Load the last 1000 lines from history |
|
1796 | 1791 | self.readline.clear_history() |
|
1797 | 1792 | stdin_encoding = sys.stdin.encoding or "utf-8" |
|
1798 | 1793 | last_cell = u"" |
|
1799 | 1794 | for _, _, cell in self.history_manager.get_tail(1000, |
|
1800 | 1795 | include_latest=True): |
|
1801 | 1796 | # Ignore blank lines and consecutive duplicates |
|
1802 | 1797 | cell = cell.rstrip() |
|
1803 | 1798 | if cell and (cell != last_cell): |
|
1804 | 1799 | if self.multiline_history: |
|
1805 | 1800 | self.readline.add_history(py3compat.unicode_to_str(cell, |
|
1806 | 1801 | stdin_encoding)) |
|
1807 | 1802 | else: |
|
1808 | 1803 | for line in cell.splitlines(): |
|
1809 | 1804 | self.readline.add_history(py3compat.unicode_to_str(line, |
|
1810 | 1805 | stdin_encoding)) |
|
1811 | 1806 | last_cell = cell |
|
1812 | 1807 | |
|
1813 | 1808 | def set_next_input(self, s): |
|
1814 | 1809 | """ Sets the 'default' input string for the next command line. |
|
1815 | 1810 | |
|
1816 | 1811 | Requires readline. |
|
1817 | 1812 | |
|
1818 | 1813 | Example: |
|
1819 | 1814 | |
|
1820 | 1815 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1821 | 1816 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1822 | 1817 | """ |
|
1823 | 1818 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1824 | 1819 | |
|
1825 | 1820 | # Maybe move this to the terminal subclass? |
|
1826 | 1821 | def pre_readline(self): |
|
1827 | 1822 | """readline hook to be used at the start of each line. |
|
1828 | 1823 | |
|
1829 | 1824 | Currently it handles auto-indent only.""" |
|
1830 | 1825 | |
|
1831 | 1826 | if self.rl_do_indent: |
|
1832 | 1827 | self.readline.insert_text(self._indent_current_str()) |
|
1833 | 1828 | if self.rl_next_input is not None: |
|
1834 | 1829 | self.readline.insert_text(self.rl_next_input) |
|
1835 | 1830 | self.rl_next_input = None |
|
1836 | 1831 | |
|
1837 | 1832 | def _indent_current_str(self): |
|
1838 | 1833 | """return the current level of indentation as a string""" |
|
1839 | 1834 | return self.input_splitter.indent_spaces * ' ' |
|
1840 | 1835 | |
|
1841 | 1836 | #------------------------------------------------------------------------- |
|
1842 | 1837 | # Things related to text completion |
|
1843 | 1838 | #------------------------------------------------------------------------- |
|
1844 | 1839 | |
|
1845 | 1840 | def init_completer(self): |
|
1846 | 1841 | """Initialize the completion machinery. |
|
1847 | 1842 | |
|
1848 | 1843 | This creates completion machinery that can be used by client code, |
|
1849 | 1844 | either interactively in-process (typically triggered by the readline |
|
1850 | 1845 | library), programatically (such as in test suites) or out-of-prcess |
|
1851 | 1846 | (typically over the network by remote frontends). |
|
1852 | 1847 | """ |
|
1853 | 1848 | from IPython.core.completer import IPCompleter |
|
1854 | 1849 | from IPython.core.completerlib import (module_completer, |
|
1855 | 1850 | magic_run_completer, cd_completer) |
|
1856 | 1851 | |
|
1857 | 1852 | self.Completer = IPCompleter(shell=self, |
|
1858 | 1853 | namespace=self.user_ns, |
|
1859 | 1854 | global_namespace=self.user_global_ns, |
|
1860 | 1855 | alias_table=self.alias_manager.alias_table, |
|
1861 | 1856 | use_readline=self.has_readline, |
|
1862 | 1857 | config=self.config, |
|
1863 | 1858 | ) |
|
1864 | 1859 | self.configurables.append(self.Completer) |
|
1865 | 1860 | |
|
1866 | 1861 | # Add custom completers to the basic ones built into IPCompleter |
|
1867 | 1862 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1868 | 1863 | self.strdispatchers['complete_command'] = sdisp |
|
1869 | 1864 | self.Completer.custom_completers = sdisp |
|
1870 | 1865 | |
|
1871 | 1866 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1872 | 1867 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1873 | 1868 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1874 | 1869 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1875 | 1870 | |
|
1876 | 1871 | # Only configure readline if we truly are using readline. IPython can |
|
1877 | 1872 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1878 | 1873 | # itself may be absent |
|
1879 | 1874 | if self.has_readline: |
|
1880 | 1875 | self.set_readline_completer() |
|
1881 | 1876 | |
|
1882 | 1877 | def complete(self, text, line=None, cursor_pos=None): |
|
1883 | 1878 | """Return the completed text and a list of completions. |
|
1884 | 1879 | |
|
1885 | 1880 | Parameters |
|
1886 | 1881 | ---------- |
|
1887 | 1882 | |
|
1888 | 1883 | text : string |
|
1889 | 1884 | A string of text to be completed on. It can be given as empty and |
|
1890 | 1885 | instead a line/position pair are given. In this case, the |
|
1891 | 1886 | completer itself will split the line like readline does. |
|
1892 | 1887 | |
|
1893 | 1888 | line : string, optional |
|
1894 | 1889 | The complete line that text is part of. |
|
1895 | 1890 | |
|
1896 | 1891 | cursor_pos : int, optional |
|
1897 | 1892 | The position of the cursor on the input line. |
|
1898 | 1893 | |
|
1899 | 1894 | Returns |
|
1900 | 1895 | ------- |
|
1901 | 1896 | text : string |
|
1902 | 1897 | The actual text that was completed. |
|
1903 | 1898 | |
|
1904 | 1899 | matches : list |
|
1905 | 1900 | A sorted list with all possible completions. |
|
1906 | 1901 | |
|
1907 | 1902 | The optional arguments allow the completion to take more context into |
|
1908 | 1903 | account, and are part of the low-level completion API. |
|
1909 | 1904 | |
|
1910 | 1905 | This is a wrapper around the completion mechanism, similar to what |
|
1911 | 1906 | readline does at the command line when the TAB key is hit. By |
|
1912 | 1907 | exposing it as a method, it can be used by other non-readline |
|
1913 | 1908 | environments (such as GUIs) for text completion. |
|
1914 | 1909 | |
|
1915 | 1910 | Simple usage example: |
|
1916 | 1911 | |
|
1917 | 1912 | In [1]: x = 'hello' |
|
1918 | 1913 | |
|
1919 | 1914 | In [2]: _ip.complete('x.l') |
|
1920 | 1915 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1921 | 1916 | """ |
|
1922 | 1917 | |
|
1923 | 1918 | # Inject names into __builtin__ so we can complete on the added names. |
|
1924 | 1919 | with self.builtin_trap: |
|
1925 | 1920 | return self.Completer.complete(text, line, cursor_pos) |
|
1926 | 1921 | |
|
1927 | 1922 | def set_custom_completer(self, completer, pos=0): |
|
1928 | 1923 | """Adds a new custom completer function. |
|
1929 | 1924 | |
|
1930 | 1925 | The position argument (defaults to 0) is the index in the completers |
|
1931 | 1926 | list where you want the completer to be inserted.""" |
|
1932 | 1927 | |
|
1933 | 1928 | newcomp = types.MethodType(completer,self.Completer) |
|
1934 | 1929 | self.Completer.matchers.insert(pos,newcomp) |
|
1935 | 1930 | |
|
1936 | 1931 | def set_readline_completer(self): |
|
1937 | 1932 | """Reset readline's completer to be our own.""" |
|
1938 | 1933 | self.readline.set_completer(self.Completer.rlcomplete) |
|
1939 | 1934 | |
|
1940 | 1935 | def set_completer_frame(self, frame=None): |
|
1941 | 1936 | """Set the frame of the completer.""" |
|
1942 | 1937 | if frame: |
|
1943 | 1938 | self.Completer.namespace = frame.f_locals |
|
1944 | 1939 | self.Completer.global_namespace = frame.f_globals |
|
1945 | 1940 | else: |
|
1946 | 1941 | self.Completer.namespace = self.user_ns |
|
1947 | 1942 | self.Completer.global_namespace = self.user_global_ns |
|
1948 | 1943 | |
|
1949 | 1944 | #------------------------------------------------------------------------- |
|
1950 | 1945 | # Things related to magics |
|
1951 | 1946 | #------------------------------------------------------------------------- |
|
1952 | 1947 | |
|
1953 | 1948 | def init_magics(self): |
|
1954 | 1949 | # FIXME: Move the color initialization to the DisplayHook, which |
|
1955 | 1950 | # should be split into a prompt manager and displayhook. We probably |
|
1956 | 1951 | # even need a centralize colors management object. |
|
1957 | 1952 | self.magic_colors(self.colors) |
|
1958 | 1953 | # History was moved to a separate module |
|
1959 | 1954 | from . import history |
|
1960 | 1955 | history.init_ipython(self) |
|
1961 | 1956 | |
|
1962 | 1957 | def magic(self, arg_s, next_input=None): |
|
1963 | 1958 | """Call a magic function by name. |
|
1964 | 1959 | |
|
1965 | 1960 | Input: a string containing the name of the magic function to call and |
|
1966 | 1961 | any additional arguments to be passed to the magic. |
|
1967 | 1962 | |
|
1968 | 1963 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1969 | 1964 | prompt: |
|
1970 | 1965 | |
|
1971 | 1966 | In[1]: %name -opt foo bar |
|
1972 | 1967 | |
|
1973 | 1968 | To call a magic without arguments, simply use magic('name'). |
|
1974 | 1969 | |
|
1975 | 1970 | This provides a proper Python function to call IPython's magics in any |
|
1976 | 1971 | valid Python code you can type at the interpreter, including loops and |
|
1977 | 1972 | compound statements. |
|
1978 | 1973 | """ |
|
1979 | 1974 | # Allow setting the next input - this is used if the user does `a=abs?`. |
|
1980 | 1975 | # We do this first so that magic functions can override it. |
|
1981 | 1976 | if next_input: |
|
1982 | 1977 | self.set_next_input(next_input) |
|
1983 | 1978 | |
|
1984 | 1979 | args = arg_s.split(' ',1) |
|
1985 | 1980 | magic_name = args[0] |
|
1986 | 1981 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1987 | 1982 | |
|
1988 | 1983 | try: |
|
1989 | 1984 | magic_args = args[1] |
|
1990 | 1985 | except IndexError: |
|
1991 | 1986 | magic_args = '' |
|
1992 | 1987 | fn = getattr(self,'magic_'+magic_name,None) |
|
1993 | 1988 | if fn is None: |
|
1994 | 1989 | error("Magic function `%s` not found." % magic_name) |
|
1995 | 1990 | else: |
|
1996 | 1991 | magic_args = self.var_expand(magic_args,1) |
|
1997 | 1992 | # Grab local namespace if we need it: |
|
1998 | 1993 | if getattr(fn, "needs_local_scope", False): |
|
1999 | 1994 | self._magic_locals = sys._getframe(1).f_locals |
|
2000 | 1995 | with self.builtin_trap: |
|
2001 | 1996 | result = fn(magic_args) |
|
2002 | 1997 | # Ensure we're not keeping object references around: |
|
2003 | 1998 | self._magic_locals = {} |
|
2004 | 1999 | return result |
|
2005 | 2000 | |
|
2006 | 2001 | def define_magic(self, magicname, func): |
|
2007 | 2002 | """Expose own function as magic function for ipython |
|
2008 | 2003 | |
|
2009 | 2004 | def foo_impl(self,parameter_s=''): |
|
2010 | 2005 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
2011 | 2006 | print 'Magic function. Passed parameter is between < >:' |
|
2012 | 2007 | print '<%s>' % parameter_s |
|
2013 | 2008 | print 'The self object is:',self |
|
2014 | 2009 | |
|
2015 | 2010 | self.define_magic('foo',foo_impl) |
|
2016 | 2011 | """ |
|
2017 | 2012 | im = types.MethodType(func,self) |
|
2018 | 2013 | old = getattr(self, "magic_" + magicname, None) |
|
2019 | 2014 | setattr(self, "magic_" + magicname, im) |
|
2020 | 2015 | return old |
|
2021 | 2016 | |
|
2022 | 2017 | #------------------------------------------------------------------------- |
|
2023 | 2018 | # Things related to macros |
|
2024 | 2019 | #------------------------------------------------------------------------- |
|
2025 | 2020 | |
|
2026 | 2021 | def define_macro(self, name, themacro): |
|
2027 | 2022 | """Define a new macro |
|
2028 | 2023 | |
|
2029 | 2024 | Parameters |
|
2030 | 2025 | ---------- |
|
2031 | 2026 | name : str |
|
2032 | 2027 | The name of the macro. |
|
2033 | 2028 | themacro : str or Macro |
|
2034 | 2029 | The action to do upon invoking the macro. If a string, a new |
|
2035 | 2030 | Macro object is created by passing the string to it. |
|
2036 | 2031 | """ |
|
2037 | 2032 | |
|
2038 | 2033 | from IPython.core import macro |
|
2039 | 2034 | |
|
2040 | 2035 | if isinstance(themacro, basestring): |
|
2041 | 2036 | themacro = macro.Macro(themacro) |
|
2042 | 2037 | if not isinstance(themacro, macro.Macro): |
|
2043 | 2038 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2044 | 2039 | self.user_ns[name] = themacro |
|
2045 | 2040 | |
|
2046 | 2041 | #------------------------------------------------------------------------- |
|
2047 | 2042 | # Things related to the running of system commands |
|
2048 | 2043 | #------------------------------------------------------------------------- |
|
2049 | 2044 | |
|
2050 | 2045 | def system_piped(self, cmd): |
|
2051 | 2046 | """Call the given cmd in a subprocess, piping stdout/err |
|
2052 | 2047 | |
|
2053 | 2048 | Parameters |
|
2054 | 2049 | ---------- |
|
2055 | 2050 | cmd : str |
|
2056 | 2051 | Command to execute (can not end in '&', as background processes are |
|
2057 | 2052 | not supported. Should not be a command that expects input |
|
2058 | 2053 | other than simple text. |
|
2059 | 2054 | """ |
|
2060 | 2055 | if cmd.rstrip().endswith('&'): |
|
2061 | 2056 | # this is *far* from a rigorous test |
|
2062 | 2057 | # We do not support backgrounding processes because we either use |
|
2063 | 2058 | # pexpect or pipes to read from. Users can always just call |
|
2064 | 2059 | # os.system() or use ip.system=ip.system_raw |
|
2065 | 2060 | # if they really want a background process. |
|
2066 | 2061 | raise OSError("Background processes not supported.") |
|
2067 | 2062 | |
|
2068 | 2063 | # we explicitly do NOT return the subprocess status code, because |
|
2069 | 2064 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2070 | 2065 | # Instead, we store the exit_code in user_ns. |
|
2071 | 2066 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2)) |
|
2072 | 2067 | |
|
2073 | 2068 | def system_raw(self, cmd): |
|
2074 | 2069 | """Call the given cmd in a subprocess using os.system |
|
2075 | 2070 | |
|
2076 | 2071 | Parameters |
|
2077 | 2072 | ---------- |
|
2078 | 2073 | cmd : str |
|
2079 | 2074 | Command to execute. |
|
2080 | 2075 | """ |
|
2081 | 2076 | cmd = self.var_expand(cmd, depth=2) |
|
2082 | 2077 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2083 | 2078 | if sys.platform == 'win32': |
|
2084 | 2079 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2085 | 2080 | with AvoidUNCPath() as path: |
|
2086 | 2081 | if path is not None: |
|
2087 | 2082 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2088 | 2083 | cmd = py3compat.unicode_to_str(cmd) |
|
2089 | 2084 | ec = os.system(cmd) |
|
2090 | 2085 | else: |
|
2091 | 2086 | cmd = py3compat.unicode_to_str(cmd) |
|
2092 | 2087 | ec = os.system(cmd) |
|
2093 | 2088 | |
|
2094 | 2089 | # We explicitly do NOT return the subprocess status code, because |
|
2095 | 2090 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2096 | 2091 | # Instead, we store the exit_code in user_ns. |
|
2097 | 2092 | self.user_ns['_exit_code'] = ec |
|
2098 | 2093 | |
|
2099 | 2094 | # use piped system by default, because it is better behaved |
|
2100 | 2095 | system = system_piped |
|
2101 | 2096 | |
|
2102 | 2097 | def getoutput(self, cmd, split=True): |
|
2103 | 2098 | """Get output (possibly including stderr) from a subprocess. |
|
2104 | 2099 | |
|
2105 | 2100 | Parameters |
|
2106 | 2101 | ---------- |
|
2107 | 2102 | cmd : str |
|
2108 | 2103 | Command to execute (can not end in '&', as background processes are |
|
2109 | 2104 | not supported. |
|
2110 | 2105 | split : bool, optional |
|
2111 | 2106 | |
|
2112 | 2107 | If True, split the output into an IPython SList. Otherwise, an |
|
2113 | 2108 | IPython LSString is returned. These are objects similar to normal |
|
2114 | 2109 | lists and strings, with a few convenience attributes for easier |
|
2115 | 2110 | manipulation of line-based output. You can use '?' on them for |
|
2116 | 2111 | details. |
|
2117 | 2112 | """ |
|
2118 | 2113 | if cmd.rstrip().endswith('&'): |
|
2119 | 2114 | # this is *far* from a rigorous test |
|
2120 | 2115 | raise OSError("Background processes not supported.") |
|
2121 | 2116 | out = getoutput(self.var_expand(cmd, depth=2)) |
|
2122 | 2117 | if split: |
|
2123 | 2118 | out = SList(out.splitlines()) |
|
2124 | 2119 | else: |
|
2125 | 2120 | out = LSString(out) |
|
2126 | 2121 | return out |
|
2127 | 2122 | |
|
2128 | 2123 | #------------------------------------------------------------------------- |
|
2129 | 2124 | # Things related to aliases |
|
2130 | 2125 | #------------------------------------------------------------------------- |
|
2131 | 2126 | |
|
2132 | 2127 | def init_alias(self): |
|
2133 | 2128 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
2134 | 2129 | self.configurables.append(self.alias_manager) |
|
2135 | 2130 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
2136 | 2131 | |
|
2137 | 2132 | #------------------------------------------------------------------------- |
|
2138 | 2133 | # Things related to extensions and plugins |
|
2139 | 2134 | #------------------------------------------------------------------------- |
|
2140 | 2135 | |
|
2141 | 2136 | def init_extension_manager(self): |
|
2142 | 2137 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
2143 | 2138 | self.configurables.append(self.extension_manager) |
|
2144 | 2139 | |
|
2145 | 2140 | def init_plugin_manager(self): |
|
2146 | 2141 | self.plugin_manager = PluginManager(config=self.config) |
|
2147 | 2142 | self.configurables.append(self.plugin_manager) |
|
2148 | 2143 | |
|
2149 | 2144 | |
|
2150 | 2145 | #------------------------------------------------------------------------- |
|
2151 | 2146 | # Things related to payloads |
|
2152 | 2147 | #------------------------------------------------------------------------- |
|
2153 | 2148 | |
|
2154 | 2149 | def init_payload(self): |
|
2155 | 2150 | self.payload_manager = PayloadManager(config=self.config) |
|
2156 | 2151 | self.configurables.append(self.payload_manager) |
|
2157 | 2152 | |
|
2158 | 2153 | #------------------------------------------------------------------------- |
|
2159 | 2154 | # Things related to the prefilter |
|
2160 | 2155 | #------------------------------------------------------------------------- |
|
2161 | 2156 | |
|
2162 | 2157 | def init_prefilter(self): |
|
2163 | 2158 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
2164 | 2159 | self.configurables.append(self.prefilter_manager) |
|
2165 | 2160 | # Ultimately this will be refactored in the new interpreter code, but |
|
2166 | 2161 | # for now, we should expose the main prefilter method (there's legacy |
|
2167 | 2162 | # code out there that may rely on this). |
|
2168 | 2163 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2169 | 2164 | |
|
2170 | 2165 | def auto_rewrite_input(self, cmd): |
|
2171 | 2166 | """Print to the screen the rewritten form of the user's command. |
|
2172 | 2167 | |
|
2173 | 2168 | This shows visual feedback by rewriting input lines that cause |
|
2174 | 2169 | automatic calling to kick in, like:: |
|
2175 | 2170 | |
|
2176 | 2171 | /f x |
|
2177 | 2172 | |
|
2178 | 2173 | into:: |
|
2179 | 2174 | |
|
2180 | 2175 | ------> f(x) |
|
2181 | 2176 | |
|
2182 | 2177 | after the user's input prompt. This helps the user understand that the |
|
2183 | 2178 | input line was transformed automatically by IPython. |
|
2184 | 2179 | """ |
|
2185 | 2180 | rw = self.displayhook.prompt1.auto_rewrite() + cmd |
|
2186 | 2181 | |
|
2187 | 2182 | try: |
|
2188 | 2183 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2189 | 2184 | # we use it and only print uncolored rewrite if we have unicode |
|
2190 | 2185 | rw = str(rw) |
|
2191 | 2186 | print >> io.stdout, rw |
|
2192 | 2187 | except UnicodeEncodeError: |
|
2193 | 2188 | print "------> " + cmd |
|
2194 | 2189 | |
|
2195 | 2190 | #------------------------------------------------------------------------- |
|
2196 | 2191 | # Things related to extracting values/expressions from kernel and user_ns |
|
2197 | 2192 | #------------------------------------------------------------------------- |
|
2198 | 2193 | |
|
2199 | 2194 | def _simple_error(self): |
|
2200 | 2195 | etype, value = sys.exc_info()[:2] |
|
2201 | 2196 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) |
|
2202 | 2197 | |
|
2203 | 2198 | def user_variables(self, names): |
|
2204 | 2199 | """Get a list of variable names from the user's namespace. |
|
2205 | 2200 | |
|
2206 | 2201 | Parameters |
|
2207 | 2202 | ---------- |
|
2208 | 2203 | names : list of strings |
|
2209 | 2204 | A list of names of variables to be read from the user namespace. |
|
2210 | 2205 | |
|
2211 | 2206 | Returns |
|
2212 | 2207 | ------- |
|
2213 | 2208 | A dict, keyed by the input names and with the repr() of each value. |
|
2214 | 2209 | """ |
|
2215 | 2210 | out = {} |
|
2216 | 2211 | user_ns = self.user_ns |
|
2217 | 2212 | for varname in names: |
|
2218 | 2213 | try: |
|
2219 | 2214 | value = repr(user_ns[varname]) |
|
2220 | 2215 | except: |
|
2221 | 2216 | value = self._simple_error() |
|
2222 | 2217 | out[varname] = value |
|
2223 | 2218 | return out |
|
2224 | 2219 | |
|
2225 | 2220 | def user_expressions(self, expressions): |
|
2226 | 2221 | """Evaluate a dict of expressions in the user's namespace. |
|
2227 | 2222 | |
|
2228 | 2223 | Parameters |
|
2229 | 2224 | ---------- |
|
2230 | 2225 | expressions : dict |
|
2231 | 2226 | A dict with string keys and string values. The expression values |
|
2232 | 2227 | should be valid Python expressions, each of which will be evaluated |
|
2233 | 2228 | in the user namespace. |
|
2234 | 2229 | |
|
2235 | 2230 | Returns |
|
2236 | 2231 | ------- |
|
2237 | 2232 | A dict, keyed like the input expressions dict, with the repr() of each |
|
2238 | 2233 | value. |
|
2239 | 2234 | """ |
|
2240 | 2235 | out = {} |
|
2241 | 2236 | user_ns = self.user_ns |
|
2242 | 2237 | global_ns = self.user_global_ns |
|
2243 | 2238 | for key, expr in expressions.iteritems(): |
|
2244 | 2239 | try: |
|
2245 | 2240 | value = repr(eval(expr, global_ns, user_ns)) |
|
2246 | 2241 | except: |
|
2247 | 2242 | value = self._simple_error() |
|
2248 | 2243 | out[key] = value |
|
2249 | 2244 | return out |
|
2250 | 2245 | |
|
2251 | 2246 | #------------------------------------------------------------------------- |
|
2252 | 2247 | # Things related to the running of code |
|
2253 | 2248 | #------------------------------------------------------------------------- |
|
2254 | 2249 | |
|
2255 | 2250 | def ex(self, cmd): |
|
2256 | 2251 | """Execute a normal python statement in user namespace.""" |
|
2257 | 2252 | with self.builtin_trap: |
|
2258 | 2253 | exec cmd in self.user_global_ns, self.user_ns |
|
2259 | 2254 | |
|
2260 | 2255 | def ev(self, expr): |
|
2261 | 2256 | """Evaluate python expression expr in user namespace. |
|
2262 | 2257 | |
|
2263 | 2258 | Returns the result of evaluation |
|
2264 | 2259 | """ |
|
2265 | 2260 | with self.builtin_trap: |
|
2266 | 2261 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2267 | 2262 | |
|
2268 | 2263 | def safe_execfile(self, fname, *where, **kw): |
|
2269 | 2264 | """A safe version of the builtin execfile(). |
|
2270 | 2265 | |
|
2271 | 2266 | This version will never throw an exception, but instead print |
|
2272 | 2267 | helpful error messages to the screen. This only works on pure |
|
2273 | 2268 | Python files with the .py extension. |
|
2274 | 2269 | |
|
2275 | 2270 | Parameters |
|
2276 | 2271 | ---------- |
|
2277 | 2272 | fname : string |
|
2278 | 2273 | The name of the file to be executed. |
|
2279 | 2274 | where : tuple |
|
2280 | 2275 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2281 | 2276 | If only one is given, it is passed as both. |
|
2282 | 2277 | exit_ignore : bool (False) |
|
2283 | 2278 | If True, then silence SystemExit for non-zero status (it is always |
|
2284 | 2279 | silenced for zero status, as it is so common). |
|
2285 | 2280 | raise_exceptions : bool (False) |
|
2286 | 2281 | If True raise exceptions everywhere. Meant for testing. |
|
2287 | 2282 | |
|
2288 | 2283 | """ |
|
2289 | 2284 | kw.setdefault('exit_ignore', False) |
|
2290 | 2285 | kw.setdefault('raise_exceptions', False) |
|
2291 | 2286 | |
|
2292 | 2287 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2293 | 2288 | |
|
2294 | 2289 | # Make sure we can open the file |
|
2295 | 2290 | try: |
|
2296 | 2291 | with open(fname) as thefile: |
|
2297 | 2292 | pass |
|
2298 | 2293 | except: |
|
2299 | 2294 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2300 | 2295 | return |
|
2301 | 2296 | |
|
2302 | 2297 | # Find things also in current directory. This is needed to mimic the |
|
2303 | 2298 | # behavior of running a script from the system command line, where |
|
2304 | 2299 | # Python inserts the script's directory into sys.path |
|
2305 | 2300 | dname = os.path.dirname(fname) |
|
2306 | 2301 | |
|
2307 | 2302 | with prepended_to_syspath(dname): |
|
2308 | 2303 | try: |
|
2309 | 2304 | py3compat.execfile(fname,*where) |
|
2310 | 2305 | except SystemExit, status: |
|
2311 | 2306 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2312 | 2307 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2313 | 2308 | # these are considered normal by the OS: |
|
2314 | 2309 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2315 | 2310 | # 0 |
|
2316 | 2311 | # > python -c'import sys;sys.exit()'; echo $? |
|
2317 | 2312 | # 0 |
|
2318 | 2313 | # For other exit status, we show the exception unless |
|
2319 | 2314 | # explicitly silenced, but only in short form. |
|
2320 | 2315 | if kw['raise_exceptions']: |
|
2321 | 2316 | raise |
|
2322 | 2317 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
2323 | 2318 | self.showtraceback(exception_only=True) |
|
2324 | 2319 | except: |
|
2325 | 2320 | if kw['raise_exceptions']: |
|
2326 | 2321 | raise |
|
2327 | 2322 | self.showtraceback() |
|
2328 | 2323 | |
|
2329 | 2324 | def safe_execfile_ipy(self, fname): |
|
2330 | 2325 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2331 | 2326 | |
|
2332 | 2327 | Parameters |
|
2333 | 2328 | ---------- |
|
2334 | 2329 | fname : str |
|
2335 | 2330 | The name of the file to execute. The filename must have a |
|
2336 | 2331 | .ipy extension. |
|
2337 | 2332 | """ |
|
2338 | 2333 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2339 | 2334 | |
|
2340 | 2335 | # Make sure we can open the file |
|
2341 | 2336 | try: |
|
2342 | 2337 | with open(fname) as thefile: |
|
2343 | 2338 | pass |
|
2344 | 2339 | except: |
|
2345 | 2340 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2346 | 2341 | return |
|
2347 | 2342 | |
|
2348 | 2343 | # Find things also in current directory. This is needed to mimic the |
|
2349 | 2344 | # behavior of running a script from the system command line, where |
|
2350 | 2345 | # Python inserts the script's directory into sys.path |
|
2351 | 2346 | dname = os.path.dirname(fname) |
|
2352 | 2347 | |
|
2353 | 2348 | with prepended_to_syspath(dname): |
|
2354 | 2349 | try: |
|
2355 | 2350 | with open(fname) as thefile: |
|
2356 | 2351 | # self.run_cell currently captures all exceptions |
|
2357 | 2352 | # raised in user code. It would be nice if there were |
|
2358 | 2353 | # versions of runlines, execfile that did raise, so |
|
2359 | 2354 | # we could catch the errors. |
|
2360 | 2355 | self.run_cell(thefile.read(), store_history=False) |
|
2361 | 2356 | except: |
|
2362 | 2357 | self.showtraceback() |
|
2363 | 2358 | warn('Unknown failure executing file: <%s>' % fname) |
|
2364 | 2359 | |
|
2365 | 2360 | def run_cell(self, raw_cell, store_history=False): |
|
2366 | 2361 | """Run a complete IPython cell. |
|
2367 | 2362 | |
|
2368 | 2363 | Parameters |
|
2369 | 2364 | ---------- |
|
2370 | 2365 | raw_cell : str |
|
2371 | 2366 | The code (including IPython code such as %magic functions) to run. |
|
2372 | 2367 | store_history : bool |
|
2373 | 2368 | If True, the raw and translated cell will be stored in IPython's |
|
2374 | 2369 | history. For user code calling back into IPython's machinery, this |
|
2375 | 2370 | should be set to False. |
|
2376 | 2371 | """ |
|
2377 | 2372 | if (not raw_cell) or raw_cell.isspace(): |
|
2378 | 2373 | return |
|
2379 | 2374 | |
|
2380 | 2375 | for line in raw_cell.splitlines(): |
|
2381 | 2376 | self.input_splitter.push(line) |
|
2382 | 2377 | cell = self.input_splitter.source_reset() |
|
2383 | 2378 | |
|
2384 | 2379 | with self.builtin_trap: |
|
2385 | 2380 | prefilter_failed = False |
|
2386 | 2381 | if len(cell.splitlines()) == 1: |
|
2387 | 2382 | try: |
|
2388 | 2383 | # use prefilter_lines to handle trailing newlines |
|
2389 | 2384 | # restore trailing newline for ast.parse |
|
2390 | 2385 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2391 | 2386 | except AliasError as e: |
|
2392 | 2387 | error(e) |
|
2393 | 2388 | prefilter_failed = True |
|
2394 | 2389 | except Exception: |
|
2395 | 2390 | # don't allow prefilter errors to crash IPython |
|
2396 | 2391 | self.showtraceback() |
|
2397 | 2392 | prefilter_failed = True |
|
2398 | 2393 | |
|
2399 | 2394 | # Store raw and processed history |
|
2400 | 2395 | if store_history: |
|
2401 | 2396 | self.history_manager.store_inputs(self.execution_count, |
|
2402 | 2397 | cell, raw_cell) |
|
2403 | 2398 | |
|
2404 | 2399 | self.logger.log(cell, raw_cell) |
|
2405 | 2400 | |
|
2406 | 2401 | if not prefilter_failed: |
|
2407 | 2402 | # don't run if prefilter failed |
|
2408 | 2403 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2409 | 2404 | |
|
2410 | 2405 | with self.display_trap: |
|
2411 | 2406 | try: |
|
2412 | 2407 | code_ast = self.compile.ast_parse(cell, filename=cell_name) |
|
2413 | 2408 | except IndentationError: |
|
2414 | 2409 | self.showindentationerror() |
|
2415 | 2410 | self.execution_count += 1 |
|
2416 | 2411 | return None |
|
2417 | 2412 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2418 | 2413 | MemoryError): |
|
2419 | 2414 | self.showsyntaxerror() |
|
2420 | 2415 | self.execution_count += 1 |
|
2421 | 2416 | return None |
|
2422 | 2417 | |
|
2423 | 2418 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2424 | 2419 | interactivity="last_expr") |
|
2425 | 2420 | |
|
2426 | 2421 | # Execute any registered post-execution functions. |
|
2427 | 2422 | for func, status in self._post_execute.iteritems(): |
|
2428 | 2423 | if not status: |
|
2429 | 2424 | continue |
|
2430 | 2425 | try: |
|
2431 | 2426 | func() |
|
2432 | 2427 | except KeyboardInterrupt: |
|
2433 | 2428 | print >> io.stderr, "\nKeyboardInterrupt" |
|
2434 | 2429 | except Exception: |
|
2435 | 2430 | print >> io.stderr, "Disabling failed post-execution function: %s" % func |
|
2436 | 2431 | self.showtraceback() |
|
2437 | 2432 | # Deactivate failing function |
|
2438 | 2433 | self._post_execute[func] = False |
|
2439 | 2434 | |
|
2440 | 2435 | if store_history: |
|
2441 | 2436 | # Write output to the database. Does nothing unless |
|
2442 | 2437 | # history output logging is enabled. |
|
2443 | 2438 | self.history_manager.store_output(self.execution_count) |
|
2444 | 2439 | # Each cell is a *single* input, regardless of how many lines it has |
|
2445 | 2440 | self.execution_count += 1 |
|
2446 | 2441 | |
|
2447 | 2442 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'): |
|
2448 | 2443 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2449 | 2444 | interactivity parameter. |
|
2450 | 2445 | |
|
2451 | 2446 | Parameters |
|
2452 | 2447 | ---------- |
|
2453 | 2448 | nodelist : list |
|
2454 | 2449 | A sequence of AST nodes to run. |
|
2455 | 2450 | cell_name : str |
|
2456 | 2451 | Will be passed to the compiler as the filename of the cell. Typically |
|
2457 | 2452 | the value returned by ip.compile.cache(cell). |
|
2458 | 2453 | interactivity : str |
|
2459 | 2454 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2460 | 2455 | run interactively (displaying output from expressions). 'last_expr' |
|
2461 | 2456 | will run the last node interactively only if it is an expression (i.e. |
|
2462 | 2457 | expressions in loops or other blocks are not displayed. Other values |
|
2463 | 2458 | for this parameter will raise a ValueError. |
|
2464 | 2459 | """ |
|
2465 | 2460 | if not nodelist: |
|
2466 | 2461 | return |
|
2467 | 2462 | |
|
2468 | 2463 | if interactivity == 'last_expr': |
|
2469 | 2464 | if isinstance(nodelist[-1], ast.Expr): |
|
2470 | 2465 | interactivity = "last" |
|
2471 | 2466 | else: |
|
2472 | 2467 | interactivity = "none" |
|
2473 | 2468 | |
|
2474 | 2469 | if interactivity == 'none': |
|
2475 | 2470 | to_run_exec, to_run_interactive = nodelist, [] |
|
2476 | 2471 | elif interactivity == 'last': |
|
2477 | 2472 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2478 | 2473 | elif interactivity == 'all': |
|
2479 | 2474 | to_run_exec, to_run_interactive = [], nodelist |
|
2480 | 2475 | else: |
|
2481 | 2476 | raise ValueError("Interactivity was %r" % interactivity) |
|
2482 | 2477 | |
|
2483 | 2478 | exec_count = self.execution_count |
|
2484 | 2479 | |
|
2485 | 2480 | try: |
|
2486 | 2481 | for i, node in enumerate(to_run_exec): |
|
2487 | 2482 | mod = ast.Module([node]) |
|
2488 | 2483 | code = self.compile(mod, cell_name, "exec") |
|
2489 | 2484 | if self.run_code(code): |
|
2490 | 2485 | return True |
|
2491 | 2486 | |
|
2492 | 2487 | for i, node in enumerate(to_run_interactive): |
|
2493 | 2488 | mod = ast.Interactive([node]) |
|
2494 | 2489 | code = self.compile(mod, cell_name, "single") |
|
2495 | 2490 | if self.run_code(code): |
|
2496 | 2491 | return True |
|
2497 | 2492 | except: |
|
2498 | 2493 | # It's possible to have exceptions raised here, typically by |
|
2499 | 2494 | # compilation of odd code (such as a naked 'return' outside a |
|
2500 | 2495 | # function) that did parse but isn't valid. Typically the exception |
|
2501 | 2496 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2502 | 2497 | # the user a traceback. |
|
2503 | 2498 | |
|
2504 | 2499 | # We do only one try/except outside the loop to minimize the impact |
|
2505 | 2500 | # on runtime, and also because if any node in the node list is |
|
2506 | 2501 | # broken, we should stop execution completely. |
|
2507 | 2502 | self.showtraceback() |
|
2508 | 2503 | |
|
2509 | 2504 | return False |
|
2510 | 2505 | |
|
2511 | 2506 | def run_code(self, code_obj): |
|
2512 | 2507 | """Execute a code object. |
|
2513 | 2508 | |
|
2514 | 2509 | When an exception occurs, self.showtraceback() is called to display a |
|
2515 | 2510 | traceback. |
|
2516 | 2511 | |
|
2517 | 2512 | Parameters |
|
2518 | 2513 | ---------- |
|
2519 | 2514 | code_obj : code object |
|
2520 | 2515 | A compiled code object, to be executed |
|
2521 | 2516 | post_execute : bool [default: True] |
|
2522 | 2517 | whether to call post_execute hooks after this particular execution. |
|
2523 | 2518 | |
|
2524 | 2519 | Returns |
|
2525 | 2520 | ------- |
|
2526 | 2521 | False : successful execution. |
|
2527 | 2522 | True : an error occurred. |
|
2528 | 2523 | """ |
|
2529 | 2524 | |
|
2530 | 2525 | # Set our own excepthook in case the user code tries to call it |
|
2531 | 2526 | # directly, so that the IPython crash handler doesn't get triggered |
|
2532 | 2527 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2533 | 2528 | |
|
2534 | 2529 | # we save the original sys.excepthook in the instance, in case config |
|
2535 | 2530 | # code (such as magics) needs access to it. |
|
2536 | 2531 | self.sys_excepthook = old_excepthook |
|
2537 | 2532 | outflag = 1 # happens in more places, so it's easier as default |
|
2538 | 2533 | try: |
|
2539 | 2534 | try: |
|
2540 | 2535 | self.hooks.pre_run_code_hook() |
|
2541 | 2536 | #rprint('Running code', repr(code_obj)) # dbg |
|
2542 | 2537 | exec code_obj in self.user_global_ns, self.user_ns |
|
2543 | 2538 | finally: |
|
2544 | 2539 | # Reset our crash handler in place |
|
2545 | 2540 | sys.excepthook = old_excepthook |
|
2546 | 2541 | except SystemExit: |
|
2547 | 2542 | self.showtraceback(exception_only=True) |
|
2548 | 2543 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2549 | 2544 | except self.custom_exceptions: |
|
2550 | 2545 | etype,value,tb = sys.exc_info() |
|
2551 | 2546 | self.CustomTB(etype,value,tb) |
|
2552 | 2547 | except: |
|
2553 | 2548 | self.showtraceback() |
|
2554 | 2549 | else: |
|
2555 | 2550 | outflag = 0 |
|
2556 | 2551 | if softspace(sys.stdout, 0): |
|
2557 | 2552 | |
|
2558 | 2553 | |
|
2559 | 2554 | return outflag |
|
2560 | 2555 | |
|
2561 | 2556 | # For backwards compatibility |
|
2562 | 2557 | runcode = run_code |
|
2563 | 2558 | |
|
2564 | 2559 | #------------------------------------------------------------------------- |
|
2565 | 2560 | # Things related to GUI support and pylab |
|
2566 | 2561 | #------------------------------------------------------------------------- |
|
2567 | 2562 | |
|
2568 | 2563 | def enable_pylab(self, gui=None, import_all=True): |
|
2569 | 2564 | raise NotImplementedError('Implement enable_pylab in a subclass') |
|
2570 | 2565 | |
|
2571 | 2566 | #------------------------------------------------------------------------- |
|
2572 | 2567 | # Utilities |
|
2573 | 2568 | #------------------------------------------------------------------------- |
|
2574 | 2569 | |
|
2575 | 2570 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
2576 | 2571 | """Expand python variables in a string. |
|
2577 | 2572 | |
|
2578 | 2573 | The depth argument indicates how many frames above the caller should |
|
2579 | 2574 | be walked to look for the local namespace where to expand variables. |
|
2580 | 2575 | |
|
2581 | 2576 | The global namespace for expansion is always the user's interactive |
|
2582 | 2577 | namespace. |
|
2583 | 2578 | """ |
|
2584 | 2579 | ns = self.user_ns.copy() |
|
2585 | 2580 | ns.update(sys._getframe(depth+1).f_locals) |
|
2586 | 2581 | ns.pop('self', None) |
|
2587 | 2582 | return formatter.format(cmd, **ns) |
|
2588 | 2583 | |
|
2589 | 2584 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
2590 | 2585 | """Make a new tempfile and return its filename. |
|
2591 | 2586 | |
|
2592 | 2587 | This makes a call to tempfile.mktemp, but it registers the created |
|
2593 | 2588 | filename internally so ipython cleans it up at exit time. |
|
2594 | 2589 | |
|
2595 | 2590 | Optional inputs: |
|
2596 | 2591 | |
|
2597 | 2592 | - data(None): if data is given, it gets written out to the temp file |
|
2598 | 2593 | immediately, and the file is closed again.""" |
|
2599 | 2594 | |
|
2600 | 2595 | filename = tempfile.mktemp('.py', prefix) |
|
2601 | 2596 | self.tempfiles.append(filename) |
|
2602 | 2597 | |
|
2603 | 2598 | if data: |
|
2604 | 2599 | tmp_file = open(filename,'w') |
|
2605 | 2600 | tmp_file.write(data) |
|
2606 | 2601 | tmp_file.close() |
|
2607 | 2602 | return filename |
|
2608 | 2603 | |
|
2609 | 2604 | # TODO: This should be removed when Term is refactored. |
|
2610 | 2605 | def write(self,data): |
|
2611 | 2606 | """Write a string to the default output""" |
|
2612 | 2607 | io.stdout.write(data) |
|
2613 | 2608 | |
|
2614 | 2609 | # TODO: This should be removed when Term is refactored. |
|
2615 | 2610 | def write_err(self,data): |
|
2616 | 2611 | """Write a string to the default error output""" |
|
2617 | 2612 | io.stderr.write(data) |
|
2618 | 2613 | |
|
2619 | 2614 | def ask_yes_no(self, prompt, default=None): |
|
2620 | 2615 | if self.quiet: |
|
2621 | 2616 | return True |
|
2622 | 2617 | return ask_yes_no(prompt,default) |
|
2623 | 2618 | |
|
2624 | 2619 | def show_usage(self): |
|
2625 | 2620 | """Show a usage message""" |
|
2626 | 2621 | page.page(IPython.core.usage.interactive_usage) |
|
2627 | 2622 | |
|
2628 | 2623 | def find_user_code(self, target, raw=True): |
|
2629 | 2624 | """Get a code string from history, file, or a string or macro. |
|
2630 | 2625 | |
|
2631 | 2626 | This is mainly used by magic functions. |
|
2632 | 2627 | |
|
2633 | 2628 | Parameters |
|
2634 | 2629 | ---------- |
|
2635 | 2630 | target : str |
|
2636 | 2631 | A string specifying code to retrieve. This will be tried respectively |
|
2637 | 2632 | as: ranges of input history (see %history for syntax), a filename, or |
|
2638 | 2633 | an expression evaluating to a string or Macro in the user namespace. |
|
2639 | 2634 | raw : bool |
|
2640 | 2635 | If true (default), retrieve raw history. Has no effect on the other |
|
2641 | 2636 | retrieval mechanisms. |
|
2642 | 2637 | |
|
2643 | 2638 | Returns |
|
2644 | 2639 | ------- |
|
2645 | 2640 | A string of code. |
|
2646 | 2641 | |
|
2647 | 2642 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
2648 | 2643 | to an object of another type. In each case, .args[0] is a printable |
|
2649 | 2644 | message. |
|
2650 | 2645 | """ |
|
2651 | 2646 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
2652 | 2647 | if code: |
|
2653 | 2648 | return code |
|
2654 | 2649 | if os.path.isfile(target): # Read file |
|
2655 | 2650 | return open(target, "r").read() |
|
2656 | 2651 | |
|
2657 | 2652 | try: # User namespace |
|
2658 | 2653 | codeobj = eval(target, self.user_ns) |
|
2659 | 2654 | except Exception: |
|
2660 | 2655 | raise ValueError(("'%s' was not found in history, as a file, nor in" |
|
2661 | 2656 | " the user namespace.") % target) |
|
2662 | 2657 | if isinstance(codeobj, basestring): |
|
2663 | 2658 | return codeobj |
|
2664 | 2659 | elif isinstance(codeobj, Macro): |
|
2665 | 2660 | return codeobj.value |
|
2666 | 2661 | |
|
2667 | 2662 | raise TypeError("%s is neither a string nor a macro." % target, |
|
2668 | 2663 | codeobj) |
|
2669 | 2664 | |
|
2670 | 2665 | #------------------------------------------------------------------------- |
|
2671 | 2666 | # Things related to IPython exiting |
|
2672 | 2667 | #------------------------------------------------------------------------- |
|
2673 | 2668 | def atexit_operations(self): |
|
2674 | 2669 | """This will be executed at the time of exit. |
|
2675 | 2670 | |
|
2676 | 2671 | Cleanup operations and saving of persistent data that is done |
|
2677 | 2672 | unconditionally by IPython should be performed here. |
|
2678 | 2673 | |
|
2679 | 2674 | For things that may depend on startup flags or platform specifics (such |
|
2680 | 2675 | as having readline or not), register a separate atexit function in the |
|
2681 | 2676 | code that has the appropriate information, rather than trying to |
|
2682 | 2677 | clutter |
|
2683 | 2678 | """ |
|
2684 | 2679 | # Close the history session (this stores the end time and line count) |
|
2685 | 2680 | # this must be *before* the tempfile cleanup, in case of temporary |
|
2686 | 2681 | # history db |
|
2687 | 2682 | self.history_manager.end_session() |
|
2688 | 2683 | |
|
2689 | 2684 | # Cleanup all tempfiles left around |
|
2690 | 2685 | for tfile in self.tempfiles: |
|
2691 | 2686 | try: |
|
2692 | 2687 | os.unlink(tfile) |
|
2693 | 2688 | except OSError: |
|
2694 | 2689 | pass |
|
2695 | 2690 | |
|
2696 | 2691 | # Clear all user namespaces to release all references cleanly. |
|
2697 | 2692 | self.reset(new_session=False) |
|
2698 | 2693 | |
|
2699 | 2694 | # Run user hooks |
|
2700 | 2695 | self.hooks.shutdown_hook() |
|
2701 | 2696 | |
|
2702 | 2697 | def cleanup(self): |
|
2703 | 2698 | self.restore_sys_module_state() |
|
2704 | 2699 | |
|
2705 | 2700 | |
|
2706 | 2701 | class InteractiveShellABC(object): |
|
2707 | 2702 | """An abstract base class for InteractiveShell.""" |
|
2708 | 2703 | __metaclass__ = abc.ABCMeta |
|
2709 | 2704 | |
|
2710 | 2705 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,436 +1,446 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Utilities for path handling. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2008-2009 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import sys |
|
19 | 19 | import tempfile |
|
20 | 20 | import warnings |
|
21 | 21 | from hashlib import md5 |
|
22 | 22 | |
|
23 | 23 | import IPython |
|
24 | 24 | from IPython.utils.process import system |
|
25 | 25 | from IPython.utils.importstring import import_item |
|
26 | 26 | from IPython.utils import py3compat |
|
27 | 27 | |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | # Code |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | |
|
32 | 32 | fs_encoding = sys.getfilesystemencoding() |
|
33 | 33 | |
|
34 | 34 | def _get_long_path_name(path): |
|
35 | 35 | """Dummy no-op.""" |
|
36 | 36 | return path |
|
37 | 37 | |
|
38 | 38 | def _writable_dir(path): |
|
39 | 39 | """Whether `path` is a directory, to which the user has write access.""" |
|
40 | 40 | return os.path.isdir(path) and os.access(path, os.W_OK) |
|
41 | 41 | |
|
42 | 42 | if sys.platform == 'win32': |
|
43 | 43 | def _get_long_path_name(path): |
|
44 | 44 | """Get a long path name (expand ~) on Windows using ctypes. |
|
45 | 45 | |
|
46 | 46 | Examples |
|
47 | 47 | -------- |
|
48 | 48 | |
|
49 | 49 | >>> get_long_path_name('c:\\docume~1') |
|
50 | 50 | u'c:\\\\Documents and Settings' |
|
51 | 51 | |
|
52 | 52 | """ |
|
53 | 53 | try: |
|
54 | 54 | import ctypes |
|
55 | 55 | except ImportError: |
|
56 | 56 | raise ImportError('you need to have ctypes installed for this to work') |
|
57 | 57 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
58 | 58 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
59 | 59 | ctypes.c_uint ] |
|
60 | 60 | |
|
61 | 61 | buf = ctypes.create_unicode_buffer(260) |
|
62 | 62 | rv = _GetLongPathName(path, buf, 260) |
|
63 | 63 | if rv == 0 or rv > 260: |
|
64 | 64 | return path |
|
65 | 65 | else: |
|
66 | 66 | return buf.value |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def get_long_path_name(path): |
|
70 | 70 | """Expand a path into its long form. |
|
71 | 71 | |
|
72 | 72 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
73 | 73 | a null operation. |
|
74 | 74 | """ |
|
75 | 75 | return _get_long_path_name(path) |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | def unquote_filename(name, win32=(sys.platform=='win32')): |
|
79 | 79 | """ On Windows, remove leading and trailing quotes from filenames. |
|
80 | 80 | """ |
|
81 | 81 | if win32: |
|
82 | 82 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): |
|
83 | 83 | name = name[1:-1] |
|
84 | 84 | return name |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | def get_py_filename(name, force_win32=None): |
|
88 | 88 | """Return a valid python filename in the current directory. |
|
89 | 89 | |
|
90 | 90 | If the given name is not a file, it adds '.py' and searches again. |
|
91 | 91 | Raises IOError with an informative message if the file isn't found. |
|
92 | 92 | |
|
93 | 93 | On Windows, apply Windows semantics to the filename. In particular, remove |
|
94 | 94 | any quoting that has been applied to it. This option can be forced for |
|
95 | 95 | testing purposes. |
|
96 | 96 | """ |
|
97 | 97 | |
|
98 | 98 | name = os.path.expanduser(name) |
|
99 | 99 | if force_win32 is None: |
|
100 | 100 | win32 = (sys.platform == 'win32') |
|
101 | 101 | else: |
|
102 | 102 | win32 = force_win32 |
|
103 | 103 | name = unquote_filename(name, win32=win32) |
|
104 | 104 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
105 | 105 | name += '.py' |
|
106 | 106 | if os.path.isfile(name): |
|
107 | 107 | return name |
|
108 | 108 | else: |
|
109 | 109 | raise IOError,'File `%s` not found.' % name |
|
110 | 110 | |
|
111 | 111 | |
|
112 | 112 | def filefind(filename, path_dirs=None): |
|
113 | 113 | """Find a file by looking through a sequence of paths. |
|
114 | 114 | |
|
115 | 115 | This iterates through a sequence of paths looking for a file and returns |
|
116 | 116 | the full, absolute path of the first occurence of the file. If no set of |
|
117 | 117 | path dirs is given, the filename is tested as is, after running through |
|
118 | 118 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
119 | 119 | |
|
120 | 120 | filefind('myfile.txt') |
|
121 | 121 | |
|
122 | 122 | will find the file in the current working dir, but:: |
|
123 | 123 | |
|
124 | 124 | filefind('~/myfile.txt') |
|
125 | 125 | |
|
126 | 126 | Will find the file in the users home directory. This function does not |
|
127 | 127 | automatically try any paths, such as the cwd or the user's home directory. |
|
128 | 128 | |
|
129 | 129 | Parameters |
|
130 | 130 | ---------- |
|
131 | 131 | filename : str |
|
132 | 132 | The filename to look for. |
|
133 | 133 | path_dirs : str, None or sequence of str |
|
134 | 134 | The sequence of paths to look for the file in. If None, the filename |
|
135 | 135 | need to be absolute or be in the cwd. If a string, the string is |
|
136 | 136 | put into a sequence and the searched. If a sequence, walk through |
|
137 | 137 | each element and join with ``filename``, calling :func:`expandvars` |
|
138 | 138 | and :func:`expanduser` before testing for existence. |
|
139 | 139 | |
|
140 | 140 | Returns |
|
141 | 141 | ------- |
|
142 | 142 | Raises :exc:`IOError` or returns absolute path to file. |
|
143 | 143 | """ |
|
144 | 144 | |
|
145 | 145 | # If paths are quoted, abspath gets confused, strip them... |
|
146 | 146 | filename = filename.strip('"').strip("'") |
|
147 | 147 | # If the input is an absolute path, just check it exists |
|
148 | 148 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
149 | 149 | return filename |
|
150 | 150 | |
|
151 | 151 | if path_dirs is None: |
|
152 | 152 | path_dirs = ("",) |
|
153 | 153 | elif isinstance(path_dirs, basestring): |
|
154 | 154 | path_dirs = (path_dirs,) |
|
155 | 155 | |
|
156 | 156 | for path in path_dirs: |
|
157 | 157 | if path == '.': path = os.getcwdu() |
|
158 | 158 | testname = expand_path(os.path.join(path, filename)) |
|
159 | 159 | if os.path.isfile(testname): |
|
160 | 160 | return os.path.abspath(testname) |
|
161 | 161 | |
|
162 | 162 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
163 | 163 | (filename, path_dirs) ) |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | class HomeDirError(Exception): |
|
167 | 167 | pass |
|
168 | 168 | |
|
169 | 169 | |
|
170 | def get_home_dir(): | |
|
171 | """Return the closest possible equivalent to a 'home' directory. | |
|
170 | def get_home_dir(require_writable=False): | |
|
171 | """Return the 'home' directory, as a unicode string. | |
|
172 | 172 | |
|
173 | 173 | * First, check for frozen env in case of py2exe |
|
174 |
* Otherwise, defer to os.path.expanduser('~') |
|
|
174 | * Otherwise, defer to os.path.expanduser('~') | |
|
175 | 175 | |
|
176 | 176 | See stdlib docs for how this is determined. |
|
177 | ||
|
178 | 177 | $HOME is first priority on *ALL* platforms. |
|
178 | ||
|
179 | Parameters | |
|
180 | ---------- | |
|
181 | ||
|
182 | require_writable : bool [default: False] | |
|
183 | if True: | |
|
184 | guarantees the return value is a writable directory, otherwise | |
|
185 | raises HomeDirError | |
|
186 | if False: | |
|
187 | The path is resolved, but it is not guaranteed to exist or be writable. | |
|
179 | 188 | """ |
|
180 | 189 | |
|
181 | 190 | # first, check py2exe distribution root directory for _ipython. |
|
182 | 191 | # This overrides all. Normally does not exist. |
|
183 | 192 | |
|
184 | 193 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
185 | 194 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
186 | 195 | root, rest = IPython.__file__.lower().split('library.zip') |
|
187 | 196 | else: |
|
188 | 197 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
189 | 198 | root=os.path.abspath(root).rstrip('\\') |
|
190 | 199 | if _writable_dir(os.path.join(root, '_ipython')): |
|
191 | 200 | os.environ["IPYKITROOT"] = root |
|
192 | 201 | return py3compat.cast_unicode(root, fs_encoding) |
|
193 | 202 | |
|
194 | 203 | homedir = os.path.expanduser('~') |
|
195 | if _writable_dir(homedir): | |
|
204 | if (not require_writable) or _writable_dir(homedir): | |
|
196 | 205 | return py3compat.cast_unicode(homedir, fs_encoding) |
|
197 | 206 | else: |
|
198 | raise HomeDirError('%s not a writable dir, set $HOME env to override' % homedir) | |
|
207 | raise HomeDirError('%s is not a writable dir, set $HOME env to override' % homedir) | |
|
199 | 208 | |
|
200 | 209 | def get_xdg_dir(): |
|
201 | 210 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
202 | 211 | |
|
203 | 212 | This is only for posix (Linux,Unix,OS X, etc) systems. |
|
204 | 213 | """ |
|
205 | 214 | |
|
206 | 215 | env = os.environ |
|
207 | 216 | |
|
208 | 217 | if os.name == 'posix': |
|
209 | 218 | # Linux, Unix, AIX, OS X |
|
210 |
# use ~/.config if |
|
|
219 | # use ~/.config if empty OR not set | |
|
211 | 220 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
212 | 221 | if xdg and _writable_dir(xdg): |
|
213 | 222 | return py3compat.cast_unicode(xdg, fs_encoding) |
|
214 | 223 | |
|
215 | 224 | return None |
|
216 | 225 | |
|
217 | 226 | |
|
218 | 227 | def get_ipython_dir(): |
|
219 | 228 | """Get the IPython directory for this platform and user. |
|
220 | 229 | |
|
221 | 230 | This uses the logic in `get_home_dir` to find the home directory |
|
222 | 231 | and then adds .ipython to the end of the path. |
|
223 | 232 | """ |
|
224 | 233 | |
|
225 | 234 | env = os.environ |
|
226 | 235 | pjoin = os.path.join |
|
227 | 236 | |
|
228 | 237 | |
|
229 | 238 | ipdir_def = '.ipython' |
|
230 | 239 | xdg_def = 'ipython' |
|
231 | 240 | |
|
232 | 241 | home_dir = get_home_dir() |
|
233 | 242 | xdg_dir = get_xdg_dir() |
|
243 | ||
|
234 | 244 | # import pdb; pdb.set_trace() # dbg |
|
235 | 245 | ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) |
|
236 | 246 | if ipdir is None: |
|
237 | 247 | # not set explicitly, use XDG_CONFIG_HOME or HOME |
|
238 | 248 | home_ipdir = pjoin(home_dir, ipdir_def) |
|
239 | 249 | if xdg_dir: |
|
240 | 250 | # use XDG, as long as the user isn't already |
|
241 | 251 | # using $HOME/.ipython and *not* XDG/ipython |
|
242 | 252 | |
|
243 | 253 | xdg_ipdir = pjoin(xdg_dir, xdg_def) |
|
244 | 254 | |
|
245 | 255 | if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir): |
|
246 | 256 | ipdir = xdg_ipdir |
|
247 | 257 | |
|
248 | 258 | if ipdir is None: |
|
249 | 259 | # not using XDG |
|
250 | 260 | ipdir = home_ipdir |
|
251 | 261 | |
|
252 | 262 | ipdir = os.path.normpath(os.path.expanduser(ipdir)) |
|
253 | 263 | |
|
254 | 264 | if os.path.exists(ipdir) and not _writable_dir(ipdir): |
|
255 | 265 | # ipdir exists, but is not writable |
|
256 | 266 | warnings.warn("IPython dir '%s' is not a writable location," |
|
257 | 267 | " using a temp directory."%ipdir) |
|
258 | 268 | ipdir = tempfile.mkdtemp() |
|
259 | 269 | elif not os.path.exists(ipdir): |
|
260 | 270 | parent = ipdir.rsplit(os.path.sep, 1)[0] |
|
261 | 271 | if not _writable_dir(parent): |
|
262 | 272 | # ipdir does not exist and parent isn't writable |
|
263 | 273 | warnings.warn("IPython parent '%s' is not a writable location," |
|
264 | 274 | " using a temp directory."%parent) |
|
265 | 275 | ipdir = tempfile.mkdtemp() |
|
266 | 276 | |
|
267 | 277 | return py3compat.cast_unicode(ipdir, fs_encoding) |
|
268 | 278 | |
|
269 | 279 | |
|
270 | 280 | def get_ipython_package_dir(): |
|
271 | 281 | """Get the base directory where IPython itself is installed.""" |
|
272 | 282 | ipdir = os.path.dirname(IPython.__file__) |
|
273 | 283 | return py3compat.cast_unicode(ipdir, fs_encoding) |
|
274 | 284 | |
|
275 | 285 | |
|
276 | 286 | def get_ipython_module_path(module_str): |
|
277 | 287 | """Find the path to an IPython module in this version of IPython. |
|
278 | 288 | |
|
279 | 289 | This will always find the version of the module that is in this importable |
|
280 | 290 | IPython package. This will always return the path to the ``.py`` |
|
281 | 291 | version of the module. |
|
282 | 292 | """ |
|
283 | 293 | if module_str == 'IPython': |
|
284 | 294 | return os.path.join(get_ipython_package_dir(), '__init__.py') |
|
285 | 295 | mod = import_item(module_str) |
|
286 | 296 | the_path = mod.__file__.replace('.pyc', '.py') |
|
287 | 297 | the_path = the_path.replace('.pyo', '.py') |
|
288 | 298 | return py3compat.cast_unicode(the_path, fs_encoding) |
|
289 | 299 | |
|
290 | 300 | def locate_profile(profile='default'): |
|
291 | 301 | """Find the path to the folder associated with a given profile. |
|
292 | 302 | |
|
293 | 303 | I.e. find $IPYTHON_DIR/profile_whatever. |
|
294 | 304 | """ |
|
295 | 305 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
296 | 306 | try: |
|
297 | 307 | pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) |
|
298 | 308 | except ProfileDirError: |
|
299 | 309 | # IOError makes more sense when people are expecting a path |
|
300 | 310 | raise IOError("Couldn't find profile %r" % profile) |
|
301 | 311 | return pd.location |
|
302 | 312 | |
|
303 | 313 | def expand_path(s): |
|
304 | 314 | """Expand $VARS and ~names in a string, like a shell |
|
305 | 315 | |
|
306 | 316 | :Examples: |
|
307 | 317 | |
|
308 | 318 | In [2]: os.environ['FOO']='test' |
|
309 | 319 | |
|
310 | 320 | In [3]: expand_path('variable FOO is $FOO') |
|
311 | 321 | Out[3]: 'variable FOO is test' |
|
312 | 322 | """ |
|
313 | 323 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
314 | 324 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
315 | 325 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
316 | 326 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
317 | 327 | # a hidden share). |
|
318 | 328 | if os.name=='nt': |
|
319 | 329 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
320 | 330 | s = os.path.expandvars(os.path.expanduser(s)) |
|
321 | 331 | if os.name=='nt': |
|
322 | 332 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
323 | 333 | return s |
|
324 | 334 | |
|
325 | 335 | |
|
326 | 336 | def target_outdated(target,deps): |
|
327 | 337 | """Determine whether a target is out of date. |
|
328 | 338 | |
|
329 | 339 | target_outdated(target,deps) -> 1/0 |
|
330 | 340 | |
|
331 | 341 | deps: list of filenames which MUST exist. |
|
332 | 342 | target: single filename which may or may not exist. |
|
333 | 343 | |
|
334 | 344 | If target doesn't exist or is older than any file listed in deps, return |
|
335 | 345 | true, otherwise return false. |
|
336 | 346 | """ |
|
337 | 347 | try: |
|
338 | 348 | target_time = os.path.getmtime(target) |
|
339 | 349 | except os.error: |
|
340 | 350 | return 1 |
|
341 | 351 | for dep in deps: |
|
342 | 352 | dep_time = os.path.getmtime(dep) |
|
343 | 353 | if dep_time > target_time: |
|
344 | 354 | #print "For target",target,"Dep failed:",dep # dbg |
|
345 | 355 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
346 | 356 | return 1 |
|
347 | 357 | return 0 |
|
348 | 358 | |
|
349 | 359 | |
|
350 | 360 | def target_update(target,deps,cmd): |
|
351 | 361 | """Update a target with a given command given a list of dependencies. |
|
352 | 362 | |
|
353 | 363 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
354 | 364 | |
|
355 | 365 | This is just a wrapper around target_outdated() which calls the given |
|
356 | 366 | command if target is outdated.""" |
|
357 | 367 | |
|
358 | 368 | if target_outdated(target,deps): |
|
359 | 369 | system(cmd) |
|
360 | 370 | |
|
361 | 371 | def filehash(path): |
|
362 | 372 | """Make an MD5 hash of a file, ignoring any differences in line |
|
363 | 373 | ending characters.""" |
|
364 | 374 | with open(path, "rU") as f: |
|
365 | 375 | return md5(py3compat.str_to_bytes(f.read())).hexdigest() |
|
366 | 376 | |
|
367 | 377 | # If the config is unmodified from the default, we'll just delete it. |
|
368 | 378 | # These are consistent for 0.10.x, thankfully. We're not going to worry about |
|
369 | 379 | # older versions. |
|
370 | 380 | old_config_md5 = {'ipy_user_conf.py': 'fc108bedff4b9a00f91fa0a5999140d3', |
|
371 | 381 | 'ipythonrc': '12a68954f3403eea2eec09dc8fe5a9b5'} |
|
372 | 382 | |
|
373 | 383 | def check_for_old_config(ipython_dir=None): |
|
374 | 384 | """Check for old config files, and present a warning if they exist. |
|
375 | 385 | |
|
376 | 386 | A link to the docs of the new config is included in the message. |
|
377 | 387 | |
|
378 | 388 | This should mitigate confusion with the transition to the new |
|
379 | 389 | config system in 0.11. |
|
380 | 390 | """ |
|
381 | 391 | if ipython_dir is None: |
|
382 | 392 | ipython_dir = get_ipython_dir() |
|
383 | 393 | |
|
384 | 394 | old_configs = ['ipy_user_conf.py', 'ipythonrc', 'ipython_config.py'] |
|
385 | 395 | warned = False |
|
386 | 396 | for cfg in old_configs: |
|
387 | 397 | f = os.path.join(ipython_dir, cfg) |
|
388 | 398 | if os.path.exists(f): |
|
389 | 399 | if filehash(f) == old_config_md5.get(cfg, ''): |
|
390 | 400 | os.unlink(f) |
|
391 | 401 | else: |
|
392 | 402 | warnings.warn("Found old IPython config file %r (modified by user)"%f) |
|
393 | 403 | warned = True |
|
394 | 404 | |
|
395 | 405 | if warned: |
|
396 | 406 | warnings.warn(""" |
|
397 | 407 | The IPython configuration system has changed as of 0.11, and these files will |
|
398 | 408 | be ignored. See http://ipython.github.com/ipython-doc/dev/config for details |
|
399 | 409 | of the new config system. |
|
400 | 410 | To start configuring IPython, do `ipython profile create`, and edit |
|
401 | 411 | `ipython_config.py` in <ipython_dir>/profile_default. |
|
402 | 412 | If you need to leave the old config files in place for an older version of |
|
403 | 413 | IPython and want to suppress this warning message, set |
|
404 | 414 | `c.InteractiveShellApp.ignore_old_config=True` in the new config.""") |
|
405 | 415 | |
|
406 | 416 | def get_security_file(filename, profile='default'): |
|
407 | 417 | """Return the absolute path of a security file given by filename and profile |
|
408 | 418 | |
|
409 | 419 | This allows users and developers to find security files without |
|
410 | 420 | knowledge of the IPython directory structure. The search path |
|
411 | 421 | will be ['.', profile.security_dir] |
|
412 | 422 | |
|
413 | 423 | Parameters |
|
414 | 424 | ---------- |
|
415 | 425 | |
|
416 | 426 | filename : str |
|
417 | 427 | The file to be found. If it is passed as an absolute path, it will |
|
418 | 428 | simply be returned. |
|
419 | 429 | profile : str [default: 'default'] |
|
420 | 430 | The name of the profile to search. Leaving this unspecified |
|
421 | 431 | The file to be found. If it is passed as an absolute path, fname will |
|
422 | 432 | simply be returned. |
|
423 | 433 | |
|
424 | 434 | Returns |
|
425 | 435 | ------- |
|
426 | 436 | Raises :exc:`IOError` if file not found or returns absolute path to file. |
|
427 | 437 | """ |
|
428 | 438 | # import here, because profiledir also imports from utils.path |
|
429 | 439 | from IPython.core.profiledir import ProfileDir |
|
430 | 440 | try: |
|
431 | 441 | pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) |
|
432 | 442 | except Exception: |
|
433 | 443 | # will raise ProfileDirError if no such profile |
|
434 | 444 | raise IOError("Profile %r not found") |
|
435 | 445 | return filefind(filename, ['.', pd.security_dir]) |
|
436 | 446 |
@@ -1,375 +1,375 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for IPython.utils.path.py""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2008 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from __future__ import with_statement |
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import shutil |
|
19 | 19 | import sys |
|
20 | 20 | import tempfile |
|
21 | 21 | from io import StringIO |
|
22 | 22 | |
|
23 | 23 | from os.path import join, abspath, split |
|
24 | 24 | |
|
25 | 25 | import nose.tools as nt |
|
26 | 26 | |
|
27 | 27 | from nose import with_setup |
|
28 | 28 | |
|
29 | 29 | import IPython |
|
30 | 30 | from IPython.testing import decorators as dec |
|
31 | 31 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
32 | 32 | from IPython.testing.tools import make_tempfile, AssertPrints |
|
33 | 33 | from IPython.utils import path, io |
|
34 | 34 | from IPython.utils import py3compat |
|
35 | 35 | |
|
36 | 36 | # Platform-dependent imports |
|
37 | 37 | try: |
|
38 | 38 | import _winreg as wreg |
|
39 | 39 | except ImportError: |
|
40 | 40 | #Fake _winreg module on none windows platforms |
|
41 | 41 | import types |
|
42 | 42 | wr_name = "winreg" if py3compat.PY3 else "_winreg" |
|
43 | 43 | sys.modules[wr_name] = types.ModuleType(wr_name) |
|
44 | 44 | import _winreg as wreg |
|
45 | 45 | #Add entries that needs to be stubbed by the testing code |
|
46 | 46 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
47 | 47 | |
|
48 | 48 | try: |
|
49 | 49 | reload |
|
50 | 50 | except NameError: # Python 3 |
|
51 | 51 | from imp import reload |
|
52 | 52 | |
|
53 | 53 | #----------------------------------------------------------------------------- |
|
54 | 54 | # Globals |
|
55 | 55 | #----------------------------------------------------------------------------- |
|
56 | 56 | env = os.environ |
|
57 | 57 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
58 | 58 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
59 | 59 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
60 | 60 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") |
|
61 | 61 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') |
|
62 | 62 | # |
|
63 | 63 | # Setup/teardown functions/decorators |
|
64 | 64 | # |
|
65 | 65 | |
|
66 | 66 | def setup(): |
|
67 | 67 | """Setup testenvironment for the module: |
|
68 | 68 | |
|
69 | 69 | - Adds dummy home dir tree |
|
70 | 70 | """ |
|
71 | 71 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
72 | 72 | # problem because that exception is only defined on Windows... |
|
73 | 73 | os.makedirs(IP_TEST_DIR) |
|
74 | 74 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | def teardown(): |
|
78 | 78 | """Teardown testenvironment for the module: |
|
79 | 79 | |
|
80 | 80 | - Remove dummy home dir tree |
|
81 | 81 | """ |
|
82 | 82 | # Note: we remove the parent test dir, which is the root of all test |
|
83 | 83 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
84 | 84 | # that non-empty directories are all recursively removed. |
|
85 | 85 | shutil.rmtree(TMP_TEST_DIR) |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def setup_environment(): |
|
89 | 89 | """Setup testenvironment for some functions that are tested |
|
90 | 90 | in this module. In particular this functions stores attributes |
|
91 | 91 | and other things that we need to stub in some test functions. |
|
92 | 92 | This needs to be done on a function level and not module level because |
|
93 | 93 | each testfunction needs a pristine environment. |
|
94 | 94 | """ |
|
95 | 95 | global oldstuff, platformstuff |
|
96 | 96 | oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd()) |
|
97 | 97 | |
|
98 | 98 | if os.name == 'nt': |
|
99 | 99 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | def teardown_environment(): |
|
103 | 103 | """Restore things that were remebered by the setup_environment function |
|
104 | 104 | """ |
|
105 | 105 | (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff |
|
106 | 106 | os.chdir(old_wd) |
|
107 | 107 | reload(path) |
|
108 | 108 | |
|
109 | 109 | for key in env.keys(): |
|
110 | 110 | if key not in oldenv: |
|
111 | 111 | del env[key] |
|
112 | 112 | env.update(oldenv) |
|
113 | 113 | if hasattr(sys, 'frozen'): |
|
114 | 114 | del sys.frozen |
|
115 | 115 | if os.name == 'nt': |
|
116 | 116 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
117 | 117 | |
|
118 | 118 | # Build decorator that uses the setup_environment/setup_environment |
|
119 | 119 | with_environment = with_setup(setup_environment, teardown_environment) |
|
120 | 120 | |
|
121 | 121 | @skip_if_not_win32 |
|
122 | 122 | @with_environment |
|
123 | 123 | def test_get_home_dir_1(): |
|
124 | 124 | """Testcase for py2exe logic, un-compressed lib |
|
125 | 125 | """ |
|
126 | 126 | sys.frozen = True |
|
127 | 127 | |
|
128 | 128 | #fake filename for IPython.__init__ |
|
129 | 129 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
130 | 130 | |
|
131 | 131 | home_dir = path.get_home_dir() |
|
132 | 132 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | @skip_if_not_win32 |
|
136 | 136 | @with_environment |
|
137 | 137 | def test_get_home_dir_2(): |
|
138 | 138 | """Testcase for py2exe logic, compressed lib |
|
139 | 139 | """ |
|
140 | 140 | sys.frozen = True |
|
141 | 141 | #fake filename for IPython.__init__ |
|
142 | 142 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
143 | 143 | |
|
144 | home_dir = path.get_home_dir() | |
|
144 | home_dir = path.get_home_dir(True) | |
|
145 | 145 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | @with_environment |
|
149 | 149 | def test_get_home_dir_3(): |
|
150 | 150 | """get_home_dir() uses $HOME if set""" |
|
151 | 151 | env["HOME"] = HOME_TEST_DIR |
|
152 | home_dir = path.get_home_dir() | |
|
152 | home_dir = path.get_home_dir(True) | |
|
153 | 153 | nt.assert_equal(home_dir, env["HOME"]) |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | @with_environment |
|
157 | 157 | def test_get_home_dir_4(): |
|
158 | 158 | """get_home_dir() still works if $HOME is not set""" |
|
159 | 159 | |
|
160 | 160 | if 'HOME' in env: del env['HOME'] |
|
161 | 161 | # this should still succeed, but we don't know what the answer should be |
|
162 | home = path.get_home_dir() | |
|
162 | home = path.get_home_dir(True) | |
|
163 | 163 | nt.assert_true(path._writable_dir(home)) |
|
164 | 164 | |
|
165 | 165 | @with_environment |
|
166 | 166 | def test_get_home_dir_5(): |
|
167 | 167 | """raise HomeDirError if $HOME is specified, but not a writable dir""" |
|
168 | 168 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') |
|
169 | nt.assert_raises(path.HomeDirError, path.get_home_dir) | |
|
169 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) | |
|
170 | 170 | |
|
171 | 171 | @with_environment |
|
172 | 172 | def test_get_ipython_dir_1(): |
|
173 | 173 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
174 | 174 | env_ipdir = os.path.join("someplace", ".ipython") |
|
175 | 175 | path._writable_dir = lambda path: True |
|
176 | 176 | env['IPYTHON_DIR'] = env_ipdir |
|
177 | 177 | ipdir = path.get_ipython_dir() |
|
178 | 178 | nt.assert_equal(ipdir, env_ipdir) |
|
179 | 179 | |
|
180 | 180 | |
|
181 | 181 | @with_environment |
|
182 | 182 | def test_get_ipython_dir_2(): |
|
183 | 183 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
184 | 184 | path.get_home_dir = lambda : "someplace" |
|
185 | 185 | path.get_xdg_dir = lambda : None |
|
186 | 186 | path._writable_dir = lambda path: True |
|
187 | 187 | os.name = "posix" |
|
188 | 188 | env.pop('IPYTHON_DIR', None) |
|
189 | 189 | env.pop('IPYTHONDIR', None) |
|
190 | 190 | env.pop('XDG_CONFIG_HOME', None) |
|
191 | 191 | ipdir = path.get_ipython_dir() |
|
192 | 192 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
193 | 193 | |
|
194 | 194 | @with_environment |
|
195 | 195 | def test_get_ipython_dir_3(): |
|
196 | 196 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
197 | 197 | path.get_home_dir = lambda : "someplace" |
|
198 | 198 | path._writable_dir = lambda path: True |
|
199 | 199 | os.name = "posix" |
|
200 | 200 | env.pop('IPYTHON_DIR', None) |
|
201 | 201 | env.pop('IPYTHONDIR', None) |
|
202 | 202 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
203 | 203 | ipdir = path.get_ipython_dir() |
|
204 | 204 | nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython")) |
|
205 | 205 | |
|
206 | 206 | @with_environment |
|
207 | 207 | def test_get_ipython_dir_4(): |
|
208 | 208 | """test_get_ipython_dir_4, use XDG if both exist.""" |
|
209 | 209 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
210 | 210 | os.name = "posix" |
|
211 | 211 | env.pop('IPYTHON_DIR', None) |
|
212 | 212 | env.pop('IPYTHONDIR', None) |
|
213 | 213 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
214 | 214 | xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython") |
|
215 | 215 | ipdir = path.get_ipython_dir() |
|
216 | 216 | nt.assert_equal(ipdir, xdg_ipdir) |
|
217 | 217 | |
|
218 | 218 | @with_environment |
|
219 | 219 | def test_get_ipython_dir_5(): |
|
220 | 220 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
221 | 221 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
222 | 222 | os.name = "posix" |
|
223 | 223 | env.pop('IPYTHON_DIR', None) |
|
224 | 224 | env.pop('IPYTHONDIR', None) |
|
225 | 225 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
226 | 226 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
227 | 227 | ipdir = path.get_ipython_dir() |
|
228 | 228 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
229 | 229 | |
|
230 | 230 | @with_environment |
|
231 | 231 | def test_get_ipython_dir_6(): |
|
232 | 232 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
233 | 233 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
234 | 234 | os.mkdir(xdg) |
|
235 | 235 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
236 | 236 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
237 | 237 | path.get_xdg_dir = lambda : xdg |
|
238 | 238 | os.name = "posix" |
|
239 | 239 | env.pop('IPYTHON_DIR', None) |
|
240 | 240 | env.pop('IPYTHONDIR', None) |
|
241 | 241 | env.pop('XDG_CONFIG_HOME', None) |
|
242 | 242 | xdg_ipdir = os.path.join(xdg, "ipython") |
|
243 | 243 | ipdir = path.get_ipython_dir() |
|
244 | 244 | nt.assert_equal(ipdir, xdg_ipdir) |
|
245 | 245 | |
|
246 | 246 | @with_environment |
|
247 | 247 | def test_get_ipython_dir_7(): |
|
248 | 248 | """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR""" |
|
249 | 249 | path._writable_dir = lambda path: True |
|
250 | 250 | home_dir = os.path.expanduser('~') |
|
251 | 251 | env['IPYTHON_DIR'] = os.path.join('~', 'somewhere') |
|
252 | 252 | ipdir = path.get_ipython_dir() |
|
253 | 253 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | @with_environment |
|
257 | 257 | def test_get_xdg_dir_1(): |
|
258 | 258 | """test_get_xdg_dir_1, check xdg_dir""" |
|
259 | 259 | reload(path) |
|
260 | 260 | path._writable_dir = lambda path: True |
|
261 | 261 | path.get_home_dir = lambda : 'somewhere' |
|
262 | 262 | os.name = "posix" |
|
263 | 263 | env.pop('IPYTHON_DIR', None) |
|
264 | 264 | env.pop('IPYTHONDIR', None) |
|
265 | 265 | env.pop('XDG_CONFIG_HOME', None) |
|
266 | 266 | |
|
267 | 267 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
268 | 268 | |
|
269 | 269 | |
|
270 | 270 | @with_environment |
|
271 | 271 | def test_get_xdg_dir_1(): |
|
272 | 272 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" |
|
273 | 273 | reload(path) |
|
274 | 274 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
275 | 275 | os.name = "posix" |
|
276 | 276 | env.pop('IPYTHON_DIR', None) |
|
277 | 277 | env.pop('IPYTHONDIR', None) |
|
278 | 278 | env.pop('XDG_CONFIG_HOME', None) |
|
279 | 279 | nt.assert_equal(path.get_xdg_dir(), None) |
|
280 | 280 | |
|
281 | 281 | @with_environment |
|
282 | 282 | def test_get_xdg_dir_2(): |
|
283 | 283 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
284 | 284 | reload(path) |
|
285 | 285 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
286 | 286 | os.name = "posix" |
|
287 | 287 | env.pop('IPYTHON_DIR', None) |
|
288 | 288 | env.pop('IPYTHONDIR', None) |
|
289 | 289 | env.pop('XDG_CONFIG_HOME', None) |
|
290 | 290 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
291 | 291 | os.makedirs(cfgdir) |
|
292 | 292 | |
|
293 | 293 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
294 | 294 | |
|
295 | 295 | def test_filefind(): |
|
296 | 296 | """Various tests for filefind""" |
|
297 | 297 | f = tempfile.NamedTemporaryFile() |
|
298 | 298 | # print 'fname:',f.name |
|
299 | 299 | alt_dirs = path.get_ipython_dir() |
|
300 | 300 | t = path.filefind(f.name, alt_dirs) |
|
301 | 301 | # print 'found:',t |
|
302 | 302 | |
|
303 | 303 | |
|
304 | 304 | def test_get_ipython_package_dir(): |
|
305 | 305 | ipdir = path.get_ipython_package_dir() |
|
306 | 306 | nt.assert_true(os.path.isdir(ipdir)) |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | def test_get_ipython_module_path(): |
|
310 | 310 | ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp') |
|
311 | 311 | nt.assert_true(os.path.isfile(ipapp_path)) |
|
312 | 312 | |
|
313 | 313 | |
|
314 | 314 | @dec.skip_if_not_win32 |
|
315 | 315 | def test_get_long_path_name_win32(): |
|
316 | 316 | p = path.get_long_path_name('c:\\docume~1') |
|
317 | 317 | nt.assert_equals(p,u'c:\\Documents and Settings') |
|
318 | 318 | |
|
319 | 319 | |
|
320 | 320 | @dec.skip_win32 |
|
321 | 321 | def test_get_long_path_name(): |
|
322 | 322 | p = path.get_long_path_name('/usr/local') |
|
323 | 323 | nt.assert_equals(p,'/usr/local') |
|
324 | 324 | |
|
325 | 325 | @dec.skip_win32 # can't create not-user-writable dir on win |
|
326 | 326 | @with_environment |
|
327 | 327 | def test_not_writable_ipdir(): |
|
328 | 328 | tmpdir = tempfile.mkdtemp() |
|
329 | 329 | os.name = "posix" |
|
330 | 330 | env.pop('IPYTHON_DIR', None) |
|
331 | 331 | env.pop('IPYTHONDIR', None) |
|
332 | 332 | env.pop('XDG_CONFIG_HOME', None) |
|
333 | 333 | env['HOME'] = tmpdir |
|
334 | 334 | ipdir = os.path.join(tmpdir, '.ipython') |
|
335 | 335 | os.mkdir(ipdir) |
|
336 | 336 | os.chmod(ipdir, 600) |
|
337 | 337 | with AssertPrints('is not a writable location', channel='stderr'): |
|
338 | 338 | ipdir = path.get_ipython_dir() |
|
339 | 339 | env.pop('IPYTHON_DIR', None) |
|
340 | 340 | |
|
341 | 341 | def test_unquote_filename(): |
|
342 | 342 | for win32 in (True, False): |
|
343 | 343 | nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py') |
|
344 | 344 | nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') |
|
345 | 345 | nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py') |
|
346 | 346 | nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') |
|
347 | 347 | nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py') |
|
348 | 348 | nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') |
|
349 | 349 | nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') |
|
350 | 350 | nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') |
|
351 | 351 | nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") |
|
352 | 352 | nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") |
|
353 | 353 | |
|
354 | 354 | @with_environment |
|
355 | 355 | def test_get_py_filename(): |
|
356 | 356 | os.chdir(TMP_TEST_DIR) |
|
357 | 357 | for win32 in (True, False): |
|
358 | 358 | with make_tempfile('foo.py'): |
|
359 | 359 | nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') |
|
360 | 360 | nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py') |
|
361 | 361 | with make_tempfile('foo'): |
|
362 | 362 | nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo') |
|
363 | 363 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
364 | 364 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) |
|
365 | 365 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
366 | 366 | true_fn = 'foo with spaces.py' |
|
367 | 367 | with make_tempfile(true_fn): |
|
368 | 368 | nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) |
|
369 | 369 | nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) |
|
370 | 370 | if win32: |
|
371 | 371 | nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) |
|
372 | 372 | nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) |
|
373 | 373 | else: |
|
374 | 374 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) |
|
375 | 375 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) |
General Comments 0
You need to be logged in to leave comments.
Login now