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